Reversi game is tough for a newbie

Ok, so I am writing a reverse game with an 8x8 array board. I have the board all complete and working, but now I am stuck. I cannot figure out how to validate the player move and then display the players piece. Also I will need to make it a two player game. Any help would be wonderful! I only have a week left to complete this.
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
178
179
  #include <iostream>
#include <string>
#include<fstream>
#include<string>
using namespace std;

const int NUM_ROWS = 8;//Global constant variable
const int NUM_COLS = 8;//Global constant variable

//Function prototypes
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()
{
	//Ask user: new game or suspended game?
	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://New Game
			{
				//If new game, initialize game board
				initialize_game_board(gameboard);
				break;
			}
		case 2://Saved Game
			{
				//cout << "In case 2";
				//If existing, read in "SAVED GAME" file
				exit (0);
			}
		case 3://Rules
			{
				ifstream infile;
				string rules;
				infile.open("Rules.txt");
				infile >> rules;
				cout << rules << endl << endl;
				infile.close();
				break;
			}
		default:
			{
				//This should never happen
				cout << "Invalid choice!\n\n";
				exit (1);
				break;
			}
	}
	//Display gameboard
	display_gameboard(gameboard);
	int row, col;
	cout << "Make your move.\n\n"
		 << "Row: ";
	cin >> row;
	while (row < 0 && row > 7)
	{
		cout << "Please enter a valid move between 0-7.\n";
		cout << "Make your move.\n\n"
		 << "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.\n\n"
		 << "Column: ";
		cin >> col;
	}
	return 0;
}

int determine_game_type_new_or_existing()
{
	int the_answer = 0;
	cout << "Welcome to Othello!\n"
		 << "-----------------------\n\n";
	do
	{
		//Ask the question
		cout << "1. New game\n"
			 << "2. Existing game\n"
			 << "3. Rules\n"
			 << "4. Quit\n\n"
			 << "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);
        }
	}
	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;
}
Last edited on

Have two variables called x and y for player position on board, those x and y would be the array index position on your board.

As an example, if the player moved right you could check column+1 to see if there was a wall (lets say a value 1) so if value 1 is at that position in your map then the player move function would prevent the move, otherwise you would update the player position and draw his new location on the map.

Hope that made sense, just woke up :)
Yes that totally makes sense. I appreciate your time. The issue I am having is translating that into actual code.

I was bored so wrote you a little example.. take no notice of the fact I used getch, its frowned upon these days and I just used it to keep things simple and easy to move around the maze. ;-)

It serves its purpose though, to show you what I meant and give you an idea how to move your player around your map.

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

#include <iostream>
#include <conio.h>	
using namespace std;

int main()
{

	char move;
	int plrX = 1, plrY = 1; // player start point

	int map[10][10] = {

		5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
		5, 1, 0, 5, 5, 5, 0, 0, 0, 5,
		5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
		5, 0, 5, 0, 5, 5, 0, 5, 5, 5,
		5, 0, 5, 0, 5, 0, 0, 0, 0, 5,
		5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
		5, 5, 5, 5, 0, 0, 5, 5, 0, 5,
		5, 0, 5, 0, 0, 0, 0, 0, 0, 5,
		5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
		5, 5, 5, 5, 5, 5, 5, 5, 5, 5,

	};

	// I know, I know _getch() is frowned
	// upon but Ive used it here just to
	// keep it simple and to demonstrate
	// the array/player question.. so 
	// before someone corrects me dont
	// bother  :)

	do
	{

		// grr i hate using system commands
		// but for this it keeps things tidy
		// so i am sure i can live with it
		// just for today  :)
		system("cls");

		// display the map, just a simple loop
		for (int down = 0; down < 10; down++) {
			for (int across = 0; across < 10; across++) {
				switch (map[down][across])
				{
					case 0:  // draw nothing
						cout << " ";
					break;
					case 1:  // player1
						cout << "@";
					break;
					case 5:  // wall
						cout << "*";
					break;
				}
			}
			cout << endl;
		}

		// read the move
		move = _getch();
		move = toupper(move);

		switch (move)
		{
			// left
				case 'L' :
					if (plrY > 1)   // if 1 we are against the wall.
					{
						if (map[plrX][plrY-1] == 0)
						{
							map[plrX][plrY] = 0;  // clear existing spot
							map[plrX][plrY - 1] = 1; // new position.
							plrY--;
						}
					}
				break;
				// right
				case 'R' :
				if (plrY < 9)   // if 9 we are against the wall.
				{
					if (map[plrX][plrY + 1] == 0)
					{
						map[plrX][plrY] = 0;  // clear existing spot
						map[plrX][plrY + 1] = 1; // new position.
						plrY++;
					}
				}
				break;
				// up
				case 'U' :
					if (plrX > 1)   // if 1 we are against the wall.
					{
						if (map[plrX-1][plrY] == 0)
						{
							map[plrX][plrY] = 0;  // clear existing spot
							map[plrX-1][plrY] = 1; // new position.
							plrX--;
						}
					}
				break;
				// down
				case 'D' :
					if (plrX < 9)   // if 9 we are against the wall.
					{
						if (map[plrX + 1][plrY] == 0)
						{
							map[plrX][plrY] = 0;  // clear existing spot
							map[plrX + 1][plrY] = 1; // new position.
							plrX++;
						}
					}
				break;
		}

	} while (move != 'Q');


	return 0;
}
God I am having such a hard time with this. I kind of see what you are doing. I'm really getting hung up on after asking what row and column they want to move their piece to, how to write the code so that it validates the move to make sure it is legal and then displaying their piece on the board. On top of that it has to flip the opponents piece that it crosses over. Ugh....Sincerely thanks again for your help.

I always find sitting at a desk with a notepad and mapping out ways to solve programming problems helps in a massive way, i.e. do dry-runs etc. before you start the actual coding.

You will be surprised how much it will help you; design and preparation plays a big part in any project even before you consider writing a line of code - It also acts as a reference in the development stages.

I wrote Othello several years ago in a self contained class and is in one of my Blu-Ray backups. However, if I were to simply locate and link that then it would be of no benefit to you whatsoever.

Programming is all about problem solving and constant training of the brain :)

You need to come up with the algorithm for checking the board for a valid move, and scanning the board to flip those which need flipping after a move. If planned out, those two functions could also be used in conjunction with a simple AI for a computer player.
OK so I have the validating of the coordinates down. The only thing I need to do is actually place the piece on the board and flip the opponents piece. Also I know I need to refresh the gameboard with the new pieces. Not sure how to do any of that. Maybe you have some insight?

Are you referring to validating if they are within a 8 x 8, or validating if the player has placed his game piece opposite one of his others?, or are you completely stuck on the whole algorithm thing?

I got the validating space done. It's just the switching whos turn it is and how to flip the opponents piece to your own. writing the code.

Well since it seems from your double post you are just looking for someone to write it for you. There are many examples on the internet (google is your friend), one is here:

http://www.java2s.com/Code/C/String/REVERSIAnOthellotypegame.htm

This is not going to help with your learning in any shape or form - the whole idea is to solve the problem yourself, that's what programming is all about. If this is your future plans to be a programmer then it will be your responsibility to find a solution.





Topic archived. No new replies allowed.