Guest User

Untitled

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. /**
  2.  * @(#)XOTest.java
  3.  *
  4.  *
  5.  * @author
  6.  * @version 1.00 2011/12/16
  7.  */
  8.  
  9.  
  10. public class XOTest {
  11.     //Board size
  12.     static int BOARD_SIZE=3;
  13.     //Draw board function
  14.     public static void printBoard(char[][] board){
  15.         for(int i=0;i<BOARD_SIZE;i++){
  16.             System.out.println("-------");
  17.             String s="";
  18.             for(int j=0;j<BOARD_SIZE;j++){
  19.                 s+="|"+board[i][j];
  20.             }
  21.             s+="|";
  22.             System.out.println(s);
  23.         }
  24.         System.out.println("-------");
  25.     }
  26.     //Check if X is the winner
  27.     public static void checkWinnerX(char[][] board){
  28.         //Winning condition
  29.         boolean winX=false;
  30.         for(int i=1;i<BOARD_SIZE;i++){
  31.             for(int j=1;j<BOARD_SIZE;j++){
  32.                 //טור
  33.                     if(board[BOARD_SIZE-i][j]=='X'){
  34.                     winX=true;
  35.                     }
  36.                     //שורה
  37.                     if(board[i][BOARD_SIZE-j]=='X'){
  38.                     winX=true;
  39.                     }
  40.                     //אלכסון
  41.                     if(board[BOARD_SIZE-i][BOARD_SIZE-j]=='X'){
  42.                         winX=true;
  43.                     }
  44.                 }
  45.         }
  46.             if(winX){
  47.             System.out.println("X wins!");
  48.         }
  49.     }
  50.  
  51.  
  52.  
  53.     public static void main(String[] args) {
  54.         //Test array
  55.         char[][] board={{'X','O','O'},{'O','X','O'},{'O','O','O'}};
  56.         printBoard(board);
  57.         checkWinnerX(board);
  58.     }
  59.  
  60.  
  61.  
  62. }
Add Comment
Please, Sign In to add comment