Answered You can hire a professional tutor to get the answer.

QUESTION

I have a program to play Reversi where a user plays against an AI(Algorithm), however I need a program where 2 algorithms play against each other.

I have a program to play Reversi where a user plays against an AI(Algorithm), however I need a program where 2 algorithms play against each other. I don't know how to change the program so that the 2 algorithms can play against each other.

These are the rules:

In this project you are required to:

1. Implement Reversi for a board size of n x n where 4 ≤ n ≤ 16 and n is always even.

2. Implement two different algorithms that will play Reversi against each other e.g. an algorithm that randomly chooses a valid move may be one of your algorithms.

You are required to develop a computer program in C++ that allows two different algorithms to compete against each other in a game of Reversi. Your code must meet the following specifications:

• Read in a list of numbers specifying board sizes from a file. • Every game must begin in the starting configuration as seen in figure 1. • Algorithm 1 will take alternating turns with algorithm 2 at placing their markers in the grid. • A marker cannot overwrite already filled position in the grid. • The list of moves made by each algorithm must be stored to an output file.

Figure 1:

4, 4

Example of output:

size = 4 (r=row, c=column)

r0c1 alg1 , r1c1

r0c0 alg2 , r1c1

r1c0 alg1 , r1c1

r2c0 alg2 , r1c0 r2c1

r3c1 alg1 , r2c2

r2c3 alg2 , r2c2

r3c2 alg1 , r2c2

r0c3 alg2 , r1c2

r1c3 alg1 , r1c2 r2c3

r0c2 alg2 , r0c1 r1c1

r3c0 alg1 , r2c1

r3c1 alg2 , r2c1

alg1 = 7

alg2 = 9

win = alg2

Code I have so far:

#include<fstream>

#include <string>

#include<string>

#include <iostream>

using namespace std;

const int NUM_ROWS = 8, NUM_COLS = 8;

int determine_game_type_new_or_existing();

void initialize_game_board(int gb [][NUM_COLS]);

void display_gameboard_row(unsigned int rownum, int gb [][NUM_COLS]);

void display_gameboard(int gb [][NUM_COLS]);

void display_cell_top();

void display_cell_side(char cell_middle);

int main()

{

       int player_choice = 0;

       int gameboard [NUM_ROWS][NUM_COLS];

       int player, opponent;

       player_choice = determine_game_type_new_or_existing();

       switch (player_choice)

       {

               case 1:

                       {

                               initialize_game_board(gameboard);

                               break;

                       }

               case 2:

                               return 0;

               default:

                       {

                               cout << "Invalid choice!nn";

                               return 1;

                               break;

                       }

       }

       display_gameboard(gameboard);

       int row, col;

       cout << "Make your move.nn"

                << "Row: ";

       cin >> row;

       while (row < 0 && row > 7)

       {

               cout << "Please enter a valid move between 0-7.n";

               cout << "Make your move.nn"

                << "Row: ";

               cin >> row;

       }

       cout << endl << endl

                << "Column: ";

       cin >> col;

       while (col < 0 && col > 7)

       {

               cout << "Please enter a valid move between 0-7.n";

               cout << "Make your move.nn"

                << "Column: ";

               cin >> col;

       }

       return 0;

}

int determine_game_type_new_or_existing()

{

       int the_answer = 0;

       cout << "Welcome to Othello!n"

                << "-----------------------nn";

       do

       {

               //Ask the question

               cout << "1. New gamen"

                        << "2. Existing gamen"

                        << "3. Quitnn"

                        << "Enter your choice: ";

               cin >> the_answer;

               cout << endl << endl;

       }while (the_answer != 1 && the_answer != 2);

       return the_answer;

}

void initialize_game_board(int gb[][NUM_COLS])

{

       //This is a nested loop to make sure every cell is empty

       //Cell Codes: 0 = empty, 1 = white piece, 2 = black piece

       for (int i = 0; i < NUM_ROWS; i++)

       {

               for (int j = 0; j < NUM_COLS; j++)

               gb[i][j] = 0;

       }

       gb[3][3] = 1;//Put down white piece

       gb[4][4] = 1;//Put down white piece

       gb[3][4] = 2;//Put down black piece

       gb[4][3] = 2;//Put down black piece

}

void display_gameboard(int gb [NUM_ROWS][NUM_COLS])

{

       for (unsigned int num = 0; num < NUM_ROWS; num++)

       {

               cout << "    ";

               display_gameboard_row(num, gb);

       }

       cout << "    ";

       for(unsigned int num = 0; num < NUM_COLS; num++)

               display_cell_top();//Displays a horizontal line

       cout << '+' << endl;

}

void display_gameboard_row(unsigned int rownum, int gb [NUM_ROWS][NUM_COLS])

{

       for (unsigned int num = 0; num < NUM_COLS; num++)

               display_cell_top();//Displays a horizontal line

       cout << '+' << endl;

       cout << "    ";

       for (unsigned int num = 0; num < NUM_COLS; num++)

       {

               display_cell_side(' ');//Displays a vertical line

       }

       cout << '|' << endl;

       cout << "    ";

       for (unsigned int col = 0; col < NUM_COLS; col++)

       {

               //char game_piece;//Either space, W or B

               if ( gb[rownum][col] == 0 )

           display_cell_side (' ');

       else if ( gb[rownum][col] == 1 )

           display_cell_side ('W');

       else if ( gb[rownum][col] == 2 )

           display_cell_side ('B');

       else

       {

           cout << "An internal error has occurred." << endl;

           //exit (2);

           //return 2;

       }

       }

       cout << '|' << endl;

       cout << "    ";

       for (unsigned int num = 0; num < NUM_COLS; num++)

               display_cell_side(' ');//Displays a vertical line

       cout << '|' << endl;

}

void display_cell_top()

{

       string cell_top = "+------";

       cout << cell_top;

}

void display_cell_side(char cell_middle)

{

       string cell_left_side = "|  ";

       //string cell_middle = " ";

       string cell_right_side = " ";

       cout << cell_left_side << cell_middle << cell_right_side;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question