need help in simple reversi game

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
char board[8][8]={{' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ','X','O',' ',' ',' '},
                  {' ', ' ', ' ','O','X',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '}};


void displayboard()
{
    cout <<"                  "<<"===============================================" << endl;
    cout <<"                  "<<"|              Othello Game        |" << endl;
    cout <<"                 "<<"===============================================" << endl;
    cout <<"" << endl;



    cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout << "8"<< "| "<<board[7][0] << " | "<<board[7][1] << " | "<<board[7][2] << " | "<<board[7][3] << " | "<<board[7][4]
         << " | "<<board[7][5] << " | "<<board[7][6]<< " | "<<board[7][7] << " | "<<board[7][8] << endl;
    cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout << "7"<< "| "<<board[6][0] << " | "<<board[6][1] << " | "<<board[6][2] << " | "<<board[6][3] << " | "<<board[6][4]
         << " | "<<board[6][5] << " | "<<board[6][6]<< " | "<<board[6][7] << " | "<<board[6][8] << endl;
    cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"6"<< "| "<<board[5][0] << " | "<<board[5][1] << " | "<<board[5][2] << " | "<<board[5][3] << " | "<<board[5][4]
         << " | "<<board[5][5] << " | "<<board[5][6]<< " | "<<board[5][7] << " | "<<board[5][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"5"<< "| "<<board[4][0] << " | "<<board[4][1] << " | "<<board[4][2] << " | "<<board[4][3] << " | "<<board[4][4]
         << " | "<<board[4][5] << " | "<<board[4][6]<< " | "<<board[4][7] << " | "<<board[4][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"4"<< "| "<<board[3][0] << " | "<<board[3][1] << " | "<<board[3][2] << " | "<<board[3][3] << " | "<<board[3][4]
         << " | "<<board[3][5] << " | "<<board[3][6]<< " | "<<board[3][7] << " | "<<board[3][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"3"<< "| "<<board[2][0] << " | "<<board[2][1] << " | "<<board[2][2] << " | "<<board[2][3] << " | "<<board[2][4]
         << " | "<<board[2][5] << " | "<<board[2][6]<< " | "<<board[2][7] << " | "<<board[2][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"2"<< "| "<<board[1][0] << " | "<<board[1][1] << " | "<<board[1][2] << " | "<<board[1][3] << " | "<<board[1][4]
         << " | "<<board[1][5] << " | "<<board[1][6]<< " | "<<board[1][7] << " | "<<board[1][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
    cout <<"1"<< "| "<<board[0][0] << " | "<<board[0][1] << " | "<<board[0][2] << " | "<<board[0][3] << " | "<<board[0][4]
         << " | "<<board[0][5] << " | "<<board[0][6]<< " | "<<board[0][7] << " | "<<board[0][8] << endl;
         cout << " |---+---+---+---+---+---+---+---|" << endl;
         cout << "  a   b   c   d   e   f   g   h " << endl;


}

void gameplay()
{

}
int main()

{
    int player = 2;
    if (player ==1)
    {
        player = 2;
    }
    else player = 1;

    char input1;
    int input2;
    displayboard();
    cout << "Player "<<player << ", please enter your input(row,column): "<<endl;
    cin >> input1 >> input2;

    if (input1 == 'a' and input2==1)
        {
            board[0][0] = 'o';
            system("cls");
            displayboard();

        }
}


Please, I need help over here. I tried to do the simple reversi game. After few days, this are all that I can do. So my question is:

1.How do I make player 1 and 2 turn to each other? I mean to make the cin input repeat until the game ends.

2.Is there any better method to fill the array.. as you can see there I just put it manually by using if statement.. that mean i need to do 8x8=64 if statemets.

3.How do I flip the O and X sign when the are surrounded by the opposite sign?

I have reviewed many sites and tic tac toe programs on the internet.. but wherever i try to imply the similar code, i just get some errors that I dont understand.

All helps, suggestions and hints about this game in C++ are appreciated.

Thanks in advance.
1) You need a loop in main that continues until one player wins.

2) Yes. Convert 'a' to an integer subscript. Hint: 'a' is decimal 97.

3) Make a bool function that checks the surrounding locations. If the function returns true, flip the location. Keep in mind the borders.
@shanz12

I wrote a Reversi game, and here's how I set up the board..
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 8; i++)
		{
			for (int j = 0; j < 8; j++)
			{
				board[i][j] = ' ';
			}
		}
		board[3][3] = 'X';
		board[4][4] = 'X';
		board[3][4] = 'O';
		board[4][3] = 'O';
You might consider using +1 and -1 for the players as there are advantages doing it that way.
A getMove() routine can deal with converting 'a' to 1 and 'b' to 2 and so on ...
In the code below enter -1 for col and row to exit.
It also shows an alternate way to display board.
To get cin to repeat until game ends you use a loop that exits when the game is done.
As to getting the pieces to flip I would have to give that some thought as I have never written a version of Reversi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
char board[8][8]={{' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '}};

int row = 1;
int col = 1;
int player = 1;

void displayboard()
{
    cout <<"  ===============================================" << endl;
    cout <<"  |                Othello Game                 |" << endl;
    cout <<"  ===============================================" << endl;

    cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    for (int j=0; j<8; j++)
    {
        cout << j+1 << " |";
        for (int i=0; i<8; i++)
        {
            cout << " " << board[i][j] << " |";
        }
        cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    }

    //cout << "    a   b   c   d   e   f   g   h " << endl;
    cout << "    1   2   3   4   5   6   7   8 " << endl;

}

void getMove()
{
    cout << "Player "<<player << ", please enter your input(row,column): " << endl;
    cin >> row >> col;

    // convert to number range 0-7 used
    row = row - 1;
    col = col - 1;
}

int main()

{

    displayboard();

    getMove();

    while (col > 0 && col < 9)
    {
        // check if in range

        // insert character
        if (player == 1)
            board[row][col] = 'O';
        else
            board[row][col] = 'X';

        //swap players
        player = -player;

        system("cls");
        displayboard();

        getMove();

    }
}

Last edited on
Oh man, I thought no one will reply this thread. I have stuck in this program for days. I will try and report my progress again here. Thanks a lot to you guys.
To Codewriter,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
char board[8][8]={{' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '},
                  {' ', ' ', ' ',' ',' ',' ',' ',' '}};

int row = 1;
int col = 1;
int player = 1;

void displayboard()
{
    cout <<"  ===============================================" << endl;
    cout <<"  |                Othello Game                 |" << endl;
    cout <<"  ===============================================" << endl;

    cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    for (int j=0; j<8; j++)
    {
        cout << j+1 << " |";
        for (int i=0; i<8; i++)
        {
            cout << " " << board[i][j] << " |";
        }
        cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    }

    //cout << "    a   b   c   d   e   f   g   h " << endl;
    cout << "    1   2   3   4   5   6   7   8 " << endl;

}

void getMove()
{
    cout << "Player "<<player << ", please enter your input(row,column): " << endl;
    cin >> row >> col;

    // convert to number range 0-7 used
    row = row - 1;
    col = col - 1;
}

int main()

{

    displayboard();

    getMove();

    while (col > 0 && col < 9)
    {
        // check if in range

        // insert character
        if (player == 1)
            board[row][col] = 'O';
        else
            board[row][col] = 'X';

        //swap players
        player = -player;

        system("cls");
        displayboard();

        getMove();

    }
}


Thanks to you. The code looks more neat and better. But in

function void get move
1
2
3
4
5
6
7
8
9
10
void getMove()
{
    cout << "Player "<<player << ", please enter your input(row,column): " << endl;
    cin >> row >> col;

    // convert to number range 0-7 used
    row = row - 1;
    col = col - 1;
}


what if row = 1,which will result tu 0; so i cant enter 1 1 as input.. any suggestions to fix this problem? Other inputs work fine anway.
You might google for other programmer's solutions.
The alpha beta routine is used to determine best moves as in a chess game.
https://gist.github.com/grantslatton/6906889
http://www.codeproject.com/Articles/24461/Reversi
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.103.494&rep=rep1&type=pdf

The rows and columns are numbered 1 to 8 from the point of view of the user but internally they are numbered 0 to 7 in the actual array. There is no board[8][8] that statement just determines the number of items which are actually numbered 0 to 7 in the computer memory.
Actually line 59 should have read,
59 while (col >= 0 && col < 8)
This should be replaced by another test to determine if the game is being ended or has ended.
The getMove() should test that the input col and row are in range. I used the out of range to end the while loop but that isn't how it should be done.
Notice the display routine uses 0 to 7 that is why the one is added to the j for the user.
28 cout << j+1 << " |";
Also the user could use a to h by converting the char to upper case and get its ascii value and subtract 65 from that value, this will convert it to the 0 to 7 range.
It is also useful to have a routine to initialize the board for when the player wants to "play again". Notice like the display routine the values are 0 to 7 not 1 to 8.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

// global variables
int row = 1;
int col = 1;
int player = 1;
char board[8][8];

void initializeBoard()
{
    for (int j=0; j<8; j++)
    {
        for (int i=0; i<8; i++)
        {
            board[i][j] = 's';
        }
    }
}

void displayboard()
{
    cout <<"  ===============================================" << endl;
    cout <<"  |                Othello Game                 |" << endl;
    cout <<"  ===============================================" << endl;

    cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    for (int j=0; j<8; j++)
    {
        cout << j+1 << " |";
        for (int i=0; i<8; i++)
        {
            cout << " " << board[i][j] << " |";
        }
        cout << endl << "  +---+---+---+---+---+---+---+---+ "<< endl;
    }

    //cout << "    a   b   c   d   e   f   g   h " << endl;
    cout << "    1   2   3   4   5   6   7   8 " << endl;

}

void getMove()
{
    cout << "Please enter your input(row,column): " << endl;
    cin >> row >> col;
    while (row<0 || row>8 or col<0 or col>8)  // loop until valid input
    {
        cout << "row or column out of range must be between 1 and 8" << endl;
        cout << "Please enter your input(row,column): " << endl;
        cin >> row >> col;
    }

    // convert to number range 0-7 used
    row = row - 1;
    col = col - 1;
}

int main()
{
    char reply = ' ';
    initializeBoard();
    displayboard();

    do
    {
        getMove();
        // insert character
        if (player == 1)
            board[row][col] = 'O';
        else
            board[row][col] = 'X';

        //swap players
        player = -player;

        system("cls");
        displayboard();
        cout << "continue game y/n ";
        cin  >> reply;
    }while (reply == 'y');
}


Sometimes it is better to start with a simpler game to get an idea how they are structured.
Here is a simple tic tac toe game where the computer makes random moves and plays against itself. You could change it to allow a user to play one side and add AI to the computer's moves.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <cstdlib>
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
using namespace std;


int b[9];
// int result;
int j;

// int gamesCount;

// test for win position
// 3 horizontal wins

int TestWin(int t)
{
  int win;
  win = 0;
  if (b[0]== t && b[1]== t && b[2]== t) {
    win = t;
  }
  if (b[3]== t && b[4]== t && b[5]== t) {
    win = t;
  }
  if (b[6]== t && b[7]== t && b[8]== t) {
    win = t;
  }
  // 3 vertical wins
  if (b[0]== t && b[3]== t && b[6]== t) {
    win = t;
  }
  if (b[1]== t && b[4]== t && b[7]== t) {
    win = t;
  }
  if (b[2]== t && b[5]== t && b[8]== t) {
    win = t;
  }
  // cross wins
  if (b[0]== t && b[4]== t && b[8]== t) {
    win = t;
  }
  if (b[2]== t && b[4]== t && b[6]== t) {
    win = t;
  }
  return win;
}

void InitializeBoard()
{
    for (int j = 0; j<9; j++)
    {
        b[j] = 0;
    }
}


// display board
void DisplayBoard()
{
     int k;
     k = 0;
     cout << "+---+---+---+" << endl;
     for (int j = 0; j<3; j++)
     {
         cout  << "|";
         for (int i = 0; i<3; i++)
         {
             if (b[k] == 0)
                cout  << "   |";
             if (b[k] == 1)
                cout  << " x |";
             if (b[k] == 2)
                cout  << " o |";
             k++;
         }
         cout << endl;
         cout  << "+---+---+---+" << endl;
     }
     cout << endl;
}

int getBoardState()
{
    int c = 1;
    int n = 0;
    for (int j = 0; j<9; j++)
    {
       if (b[j] == 1)
          n = n + c;
       if (b[j] == 2)
          n = n + c*2;
       c = c * 10;
     }
     return n;
}


void MakeAMove(int t)
{
  int choice = 0;
  /* initialize random seed: */
  srand (time(NULL));
  choice = rand()%9;  // 0 to 8 returned
  while (b[choice]!= 0)
  {
    choice = rand()%9;
  }
  b[choice] = t;
}

int PlayAGame()
{
    int gamesCount = 0;
    int result = 0;   // 1 = x win , 2 = y win and 3 = draw

    InitializeBoard();

    DisplayBoard();

    while (result == 0)
    {
      gamesCount = gamesCount + 1;

      MakeAMove(1);
      result = TestWin(1);
      DisplayBoard();

      if (result != 1  && gamesCount < 5)
      {
        MakeAMove(2);
        result = TestWin(2);
        DisplayBoard();
      }

      if (result == 0 && gamesCount == 5)
      {
        result = 3;
      }

      cout << getBoardState() << endl;

    } // end while

    return result;
}

int main(int argc, char *argv[])
{
    int result;
    char reply;
    do
    {
        for (int z = 0; z < 3; z++)
        {
            result = PlayAGame();
            // print result
            if (result == 1)
            {
              cout  << "x won" << endl;
            }
            else if (result == 2)
            {
                cout  << "o won" << endl;
            }
            else if (result == 3)
            {
                cout  << "DRAW" << endl;
            }
        }
        cout << "Do you want to continue? y/n ";
        cin  >> reply;
    }while (reply == 'y');

return 0;
}
Last edited on
Topic archived. No new replies allowed.