Friday, October 2, 2015

[7.8] Othello

1. Example

Game:
1.strat
2.run
3.check rule
4.add result


    A     B   C     D     E      F     G     H
8   
7   
6
5                      W     B
4                      B     W
3
2  
1  


Start:     B W
             W B
Flip1:    W B W --> W W W
Flip2:        W                W
                  B      -->      W
                 W                 W


Rule1: Each othello piece is white on one side and black on the other(see start).

Rule2: When a piece is surrounded by its opponents on both the left and right sides(see Flip 1), or both the top and bottom(see Flip 2), it is said to be captures and its color is flipped.

Rule3: On your turn, you must capture at least one of your opponent's pieces(at least flip1 or flip2 happen).

Rule4: The game ends when wither user has no more valid moves, and the win is assigned to the person with the most pieces.


2. Implementation
Othello has three major steps:
1. Game() which would be the main function to manage all the activity in the game:
2. Initialize the game which will be done by constructor
3. Get first user input
4. Validate the input
5. Change board configuration
6. Check if someone has won the game
7. Get second input
8. Validate the input
9. Change the board configuration
10.Check if someone has won the game
private void getMove( int color ) throws Exception{...}
try 
   getMove(black); valid = true;
 } 
catch (Exception e) 
   System.out.println("Enter a valid coordinate!"); 
}
public class Question
{



     private final int white = 1;
     private final int black = 2;
     private int[][] board;


  

     // Set up the board in the standard othello starting positions, 
     // and starts the game
     public void start() {....}
     

     


     // The actual game runs continuously until a player wins
     public void game()
     {

         printBoard();


         while ( won() == 0 )
         {
              boolean valid = false;
              while ( !valid )
              {
                    try
                    {
                        getMove(black);
                        valid = true;
                    }
                    catch (Exception e)
                    {
                        System.out.println("Enter a valid coordinate!");
                    }
              }
              valid = false;
              while (  !valid ) 
              {
                    try
                    {
                        getMove(white);
                        valid = true;
                    }
                    catch
                    {
                        System.out.println("Enter a valid coordinate !");
                    }
              }
              
              printBoard(); 
             
         }


         if ( won() != 3 )
            System.out.println( won() ==1 ? "white": "black" + " won !");
         else 
            System.out.println("It's a draw !");


     }






     // NOTE(GET INPUT): Get the input - a move
     // Prompts the player for a move and the coordinates for the move. 
     // throws an exception if the input is not valid or if the entered 
     // coordinates do not make a valid move
     private void getMove( int color ) throws Exception{...}

     


     // NOTE(CHECK RULE): 1. Validate the move
     // returns if a move at coordinate (x,y) is a valid move for the specified player
     private boolean isValid(int color, int x, int y){...}



     // NOTE(ADD SUB RESULT): 2. Configure the board
     // Adds the move onto the board, and the pieces gained from that 
     // move. Assume the move is valid
     public void add(int x, int y, int color){...}




     // NOTE(CHECK RULE): 3. Check if someone Won
     // Check which color has more pieces on the board, count the pieces
     // Returns the winner, if any. If there are no winners, returns 0
     private int won()
     {
          if ( !canGo(white) && !canGo(black) )
          {


                int count = 0;
                for (int i = 0 ; i < 8 ; i++ )
                {
                     for (int j = 0 ; j < 8; j ++)
                     {
                          if (board[i][j] == white)
                          {
                                count++;
                          }
                          if (board[i][j] == black)
                          {
                               count--;
                          }
                     }
                }


   
                if ( count > 0 ) return white; 
                if ( count < 0 ) return black;
                // count == 0 --> draw
                return 3; 
                

          }


          // Still canGo()
          return 0;


     }
     



     // NOTE(CHECK RULE): 
     // Returns whether the player of the specified color has a valid 
     //  move in his turn. This will return false when 
     //  1. none of his pieces are present (no piece remaining)
     //  2. none of his moves result in him gaining new pieces
     //  3. the board is filled up
     private boolean canGo(int color){...}



}
3. Similar ones

No comments:

Post a Comment