import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         char[][] chessboard = new char[7][7];
         int x, y;

         for(int i = 0; i < 7; i++)
             for(int j = 0; j < 7; j++)
                 chessboard[i][j] = ' ';
         for(int i = 0; i < 7; i += 2)
             for(int j = 0; j < 7; j++)
                 chessboard[i][j] = '-';
         for(int i = 1; i < 7; i += 2)
             for(int j = 0; j < 7; j += 2)
                 chessboard[i][j] = '|';

         display(chessboard);

         while(true)
         {
             System.out.print("Enter a row(1, 2, or 3) for player X: ");
             x = input.nextInt();
             System.out.print("Enter a column(1, 2, or 3) for player X: ");
             y = input.nextInt();
             putChess(chessboard, x, y, 'X');
             display(chessboard);

             if(judge(chessboard) == true)
             {
                 System.out.println("X player won");
                 break;
             }

             System.out.print("Enter a row(1, 2, or 3) for player O: ");
             x = input.nextInt();
             System.out.print("Enter a column(1, 2, or 3) for player O: ");
             y = input.nextInt();
             putChess(chessboard, x, y, 'O');
             display(chessboard);

             if(judge(chessboard) == true)
             {
                 System.out.println("O player won");
                 break;
             }

             if(isFull(chessboard))
             {
                 System.out.println("Draw");
                 break;
             }
         }
     }

     public static void display(char[][] array)
     {
         for(int i = 0; i < 7; i++)
         {
             for(int j = 0; j < 7; j++)
                 System.out.print(array[i][j]);
             System.out.println();
         }
     }

     public static void putChess(char[][] array, int x, int y, char ch)
     {
         array[2 * x - 1][2 * y - 1] = ch;
     }

     public static boolean judge(char[][] array)
     {
         for(int i = 1; i < 7; i += 2)
         {
             if(array[i][1] == array[i][3] && array[i][1] == array[i][5] && array[i][1] != ' ')
                 return true;
             if(array[1][i] == array[3][i] && array[1][i] == array[5][i] && array[1][i] != ' ')
                 return true;
         }
         if(array[1][1] == array[3][3] && array[1][1] == array[5][5] && array[1][1] != ' ')
             return true;
         if(array[5][1] == array[3][3] && array[5][1] == array[1][5] && array[5][1] != ' ')
             return true;
         return false;

     }

     public static boolean isFull(char[][] array)
     {
         for(int i = 0; i < array.length; i++)
             for(int j = 0; j < array[0].length; j++)
                 if(array[i][j] == ' ')
                     return false;
         return true;
     }
 }

HW7.9的更多相关文章

  1. HW7.18

    public class Solution { public static void main(String[] args) { int[][] m = {{1, 2}, {3, 4}, {5, 6} ...

  2. HW7.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. HW7.16

    import java.util.Arrays; public class Solution { public static void main(String[] args) { int row = ...

  4. HW7.15

    public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...

  5. HW7.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. HW7.13

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. HW7.12

    import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...

  8. HW7.11

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  9. HW7.10

    public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...

随机推荐

  1. Object-C中emoji与json的问题

    遇到一个问题,要储存iOS键盘输出的emoji表情到MySQL,我知道用blob+utf8是可以存的.但是现在我的这张表已经太大了,不可能去改类型.所以就想把emoji表情匹配出来,替换掉,再存.但是 ...

  2. 将ANGULAR与后端请求结合

    简单的结合,却是很多应用的基础.RESTFUL就此而生.瘦服务,富客户. <!DOCTYPE html> <html lang="en" ng-app=" ...

  3. hdu 3929 Big Coefficients 容斥原理

    看懂题目,很容易想到容斥原理. 刚开始我用的是二进制表示法实现容斥原理,但是一直超时.后来改为dfs就过了…… 代码如下: #include<iostream> #include<s ...

  4. linux ubuntu删除引导 grub出现错误解决方案

    使用u盘启动PE系统 找到diskgenius软件,点击: 硬盘->重建主引导记录

  5. hbase命令备忘

    http://www.cnblogs.com/linjiqin/archive/2013/03/08/2949339.html HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase ...

  6. Android工具:延展图片NinePatch

    NinePatch能够对.png图片进行处理,生成一个.9.png格式的图片,图像拉伸操作时,图片就会有失真,而.9.png是Android里所支持的一种特殊的图片格式,可以实现部分拉伸. 制作图片方 ...

  7. 第二个C语言代码

    有问题,还没找出哪里出错了       输入一串字符,问号结束 统计1~9各出现的次数 ******************************************************** ...

  8. 排查Java线上服务故障的方法和实例分析

    前言 作为在线系统负责人或者是一个技术专家,你可能刚刚接手一个项目就需要处理紧急故障,或者被要求帮忙处理一些紧急的故障,这个时候的情景是: (1)你可能对这个业务仅仅是听说过,而不怎么真正了解: (2 ...

  9. ARM处理器全解析:A8/A9/A15都是什么?

    前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列产品,以此来扩大ARM在高性能与低功耗领域的领先地位,进一步抢占移动终端市场份额.Cortex-A50是继Cortex-A15 ...

  10. C语言中的位拷贝与值拷贝浅谈(转载)

    注:C语言实现的PHP变量的赋值过程中,就涉及到了 深拷贝和浅拷贝 位拷贝拷贝的是地址(也叫浅拷贝),而值拷贝则拷贝的是内容(深拷贝).深拷贝和浅拷贝可以简单理解为:如果一个类拥有资源,当这个类的对象 ...