Advertisement
pro-themes

Board (Java)

Jan 29th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. public class Board {
  2.     private char[] board = new char[9];
  3.    
  4.     public Board(){ }
  5.    
  6.     public void displayBoard(){
  7.         System.out.println("[ " + board[0] + " ]" + "[ " + board[1] + " ]" + "[ " + board[2] + " ]");
  8.         System.out.println("[ " + board[3] + " ]" + "[ " + board[4] + " ]" + "[ " + board[5] + " ]");
  9.         System.out.println("[ " + board[6] + " ]" + "[ " + board[7] + " ]" + "[ " + board[8] + " ]");
  10.         System.out.println();
  11.     }
  12.    
  13.     public void setSpace(int space, char letter){
  14.         board[space] = letter;
  15.     }
  16.    
  17.     public char getSpace(int space){
  18.         return this.board[space];
  19.     }
  20.    
  21.     public boolean notOccupied(int space){
  22.         return (board[space] == '-'); //true if space contains "-"
  23.     }
  24.    
  25.     public void clear(){
  26.         //clear the board
  27.         for(int i = 0; i < 9; i++){
  28.             board[i] = '-';
  29.         }
  30.     }
  31.    
  32.     public boolean checkGame(Board board){ //check when game over
  33.         int total = 0;
  34.         //Check rows for 3 X or O in a row
  35.         for(int i = 0; i < 3; i++){ //row 1
  36.             total += (int)board.getSpace(i);
  37.             if(total == 237 || total == 264)
  38.                 return true;
  39.         }
  40.         total = 0;
  41.         for(int i = 3; i < 6; i++){ //row 2
  42.             total += (int)board.getSpace(i);
  43.             if(total == 237 || total == 264)
  44.                 return true;
  45.         }
  46.         total = 0;
  47.         for(int i = 6; i < 9; i++){ //row 3
  48.             total += (int)board.getSpace(i);
  49.             if(total == 237 || total == 264)
  50.                 return true;
  51.         }
  52.         total = 0;
  53.         //Check columns for 3 X or O in a row
  54.         for(int i = 0; i < 7; i += 3){ //column 1
  55.             total += (int)board.getSpace(i);
  56.             if(total == 237 || total == 264)
  57.                 return true;
  58.         }
  59.         total = 0;
  60.         for(int i = 1; i < 8; i += 3){ //column 2
  61.             total += (int)board.getSpace(i);
  62.             if(total == 237 || total == 264)
  63.                 return true;
  64.         }
  65.         total = 0;
  66.         for(int i = 2; i < 10; i += 3){ //column 3
  67.             total += (int)board.getSpace(i);
  68.             if(total == 237 || total == 264)
  69.                 return true;
  70.         }
  71.         total = 0;
  72.         //check diagonal-right for 3 X or O in a row
  73.         for(int i = 0; i < 10; i += 4){
  74.             total += (int)board.getSpace(i);
  75.             if(total == 237 || total == 264)
  76.                 return true;
  77.         }
  78.         total = 0;
  79.         //check diagonal-left for 3 X or O in a row
  80.         for(int i = 2; i < 7; i += 2){
  81.             total += (int)board.getSpace(i);
  82.             if(total == 237 || total == 264)
  83.                 return true;
  84.         }
  85.         total = 0;
  86.        
  87.         return false; //Default
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement