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. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  2. 让wordpress投稿作者在后台只看到自己的文章

    wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...

  3. c#做动态(gif)中文验证码

    无意中在国外论坛发现一个gif动画类,我使用它来制作了一个动态验证码 : 一:首先新建一个类库 1:新建AnimatedGifEncoder类 using System; using System.C ...

  4. POJ3096Surprising Strings(map)

    题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...

  5. android 使用Activity做窗口弹出(模拟Dialog)

    我们下面使用Activity,模拟一个dialog: 首先看布局: <?xml version="1.0" encoding="utf-8"?> & ...

  6. MySQL追加注释或者大量修改注释

     MySQL追加注释或者大量修改注释 2016-01-25 20:28:05 分类: MySQL MySQL 5.6.14 之前一个项目比较仓促,开发给的建表语句没有注释.现在要补全注释信息.但是My ...

  7. Linux下的动态连接库及其实现机制

    Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...

  8. javeWeb常用快捷键 Junit for changeableargs enumn reflect

    *1 工具常用的快捷键 1) Eclipse和MyEclipse,IBM,2001,Java编写,开源,跨平台跨语言   2)Alt+/快速内容提示   3)Ctrl+1快速修补错误   4)Syso ...

  9. C# 自定义光标 WaitCursor

    一种: 把图像文件放到项目的文件夹中 1 如果图像文件是.cur格式: Cursor cur=new Cursor(文件名); this.cursor=cur; 两句话 就完事 2 如果图像文件是其他 ...

  10. GPRS连接失败问题

    备注:采用的是SIMCOM的SIM900模块 1. GPRS连接失败问题 2013.08.06测试9台C2000两次,两次之间是机器拿开电池,间隔40分钟左右,每次都出现一台(但不是同一台): ⑴9台 ...