HW7.9

HW7.9

HW7.9

HW7.9

 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;
     }
 }
上一篇:Hadoop源码篇---解读Mapprer源码Input输入


下一篇:再谈AbstractQueuedSynchronizer:共享模式与基于Condition的等待/通知机制实现