bushra-rafia / Othello-Reversi-AI-Game

I implemented this Othello or reversi AI (Artificial Intelligence) game in C++ language. This is my AI lab project. Here, I implemented its Human vs Computer player. [8x8 Board size]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Othello-Reversi-AI-Game

I implemented this Othello or reversi AI (Artificial Intelligence) game in C++ language. This is my AI lab project. Here, I implemented its Human vs Computer player. [8x8 Board size]

Othello Game:

Othello is a strategy board game played between 2 players. There are sixty-four identical game pieces. Initially, the board will start with 2 black pieces and 2 white pieces at the center of the board. The goal for each player is to make pieces of their color constitute a majority of the pieces on the board at the end of the game, by turning over as many of their opponent's pieces as possible. For this, both human and computer players want to take the four corners points of the board.

Setup:

I used an 8x8 array of char to represent the board. Here, the pieces are X’s and O’s instead of black and white. At first the game will start with the human player (X). And then after every iteration the player will be swapped. It will continue until the game is over. The game will be finished when there will be no empty position (.) on the beard. After every iteration i showed the score of both human and computer player in output. And I implemented the game in C program.

For Human Move:

Now if the current player is human(X) then it will take the input of row and coloumn. Then from this input, first it will check the position is valid or not with the validMove function .The function for determining whether a move is actually valid or not before trying to make the move. A move is valid if the board location is unoccupied and it can flip pieces in any of the eight directions. This function uses a helper functions. checkFlip functions will looks in one of the 8 directions (left, right, up, down, upleft, upright, downleft, downright) to see if adjacent opponent pieces (O) are sandwiched between human pieces (X). The -1, 0, and 1 control which direction we are looking. For example for looking to the right, the x change is 1 but the y change is 0. If we are looking to the left, the x change is -1 and the y change is 0, etc. If so, it returns true. If not, it returns false. True means the move is valid in this direction. If the move is valid move then, it will make the move in that position and also filp the opponent pieces with human pieces .Also, there are some cases where no move is possible and here the human player forfeit the turn. In this case human player will enter -1 to skip to the next player (Computer Player). Here showing if Human player wants to make a move on 4th column and 2nd row then it’s a valid move. Because the opponent player (O) are sandwiched between human pieces (X) upside. So, the human player can able to place the move and the opponent pieces are also flipped all the upside opponent pieces until to reach the human pieces on that side. Similarly, if Human player wants to make a move on 7th column and 5th row then it’s a valid move. Because the opponent player (O) are sandwiched between human pieces (X) for downleft corner side. So, the human player can able to place the move and the opponent pieces are also flipped.

For computer Move:

For a computer player valid move. I did this by picking a random move from the move list. I hard-coded this to pick a random move for O. For computer move it will first call the Getmovelist function. And it will then call the validMove function. A move is valid if the board location is unoccupied and it can flip pieces in any of the eight directions. This function uses a helper functions. checkFlip functions will looks in one of the 8 directions (left, right, up, down, upleft, upright, downleft, downright) to see if adjacent opponent pieces (X) are sandwiched between computer pieces (O).From this valid move position for a computer player will then randomly give a valid move on the board. If any point there is no valid move for a computer player then it will just swapped, and then it will be a human player turn. Similarly by playing rest of the part of the game both human and computer player can able to win in my game. But when playing the game a human player always wants to lose the computer player. So, if a human player follow some tricks then a human can able to lose the computer player.

About

I implemented this Othello or reversi AI (Artificial Intelligence) game in C++ language. This is my AI lab project. Here, I implemented its Human vs Computer player. [8x8 Board size]


Languages

Language:C++ 100.0%