Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trying to make a reversei game also known as Othello. Having trouble with the ApplyMove function. This is essentially the algorithm im going for but

Trying to make a reversei game also known as Othello. Having trouble with the ApplyMove function.

This is essentially the algorithm im going for but cant seem to get my head around.

1. Choose a Direction 2. Start at (r,c) 3. Take 1 step in direction. If we find enemy piece, increase counter, repeat step 3. 4. Why did we stop? a) Blank/ Out of bounds return to Step 1. b) Self: if counter == 0, return to step 1 else, walk back & flip. i. Take 1 step backward. ii. Change square to self iii. Repeat once for each enemy found. iv. Return to step 1.

Here is what i got so far to give a little context:

void ApplyMove(OthelloMatrix &board, int row, int col, char currentPlayer) { int direction = 0; while (currentPlayer = 'b')//blackmove { std::cout << "this sucks" << std::endl; switch (direction) { case 0: if (board[col - 1][row] = -1) //north { board[col - 1][row] = 1; currentPlayer = 'w'; direction++; } else { break; } case 1: if (board[col - 1][row + 1] == -1)//northeast { board[col - 1][row + 1] = 1; currentPlayer = 'w'; direction++; } else { break; } case 2: if (board[col][row + 1] == -1)//east { board[col][row + 1] = 1; currentPlayer = 'w'; direction++; } else { break; } case 3: if (board[col + 1][row + 1] == -1)//southeast { board[col + 1][row + 1] = 1; currentPlayer = 'w'; direction++; } else { break; } case 4: if (board[col + 1][row] == -1)//south { board[col + 1][row] = 1; currentPlayer = 'w'; direction++; } else { break; } case 5: if (board[col + 1][row - 1] == -1)//southwest { board[col + 1][row - 1] = 1; currentPlayer = 'w'; direction++; } else { break; } case 6: if (board[col][row - 1] == -1)//west { board[col][row - 1] = 1; currentPlayer = 'w'; direction++; } else { break; } case 7: if (board[col - 1][row - 1] == -1)//northwest { board[col - 1][row - 1] = 1; currentPlayer = 'w'; direction++; } else { break; } } currentPlayer = 'w'; } The algorithm that i posted on top is the general idea of how it should loop but im not quite sure how to implement it. So generally it should flip for the black piece player and the white piece player but so far my code only allows me to flip a single white piece that are surrounded by my black piece. So i need a way to flip as many enemy pieces that are surrounded. So if i had 2 or more white pieces surrounded by black pieces it should flip everything in between my black pieces. I need it to loop efficiently from white to black moves. My code checks for directions and fills one piece towards that direction but like i said i need it to keep checking that direction until it either hits out of bounds, the same color piece as the player, or if it is a blank square.

I will provide the header and main file to give you a little more context:

//main-------------------------- #include "OthelloGame.h" using namespace std;

char cP; int colPtr, rowPtr, token = 0;

std::array, BOARD_SIZE> board = { 0 };

int main() { board[3][3] = -1; board[3][4] = 1; board[4][3] = 1; board[4][4] = -1; board[5][4] = -1;//testing moves board[6][4] = -1;//testing moves

PrintBoard(board); // Game Start. //Black Player Turn. do {

cout << "Black Players Turn. Please give coordinates:(columns, row)" << endl; cP = 'b'; GetMove(&colPtr, &rowPtr); if (colPtr == -1 && rowPtr == -1) { token++; cout << "Player Passed Turn." << endl; } } while (!InBounds(rowPtr, colPtr) || !IsValidMove(board, rowPtr, colPtr)); board[rowPtr][colPtr] = 1; ApplyMove(board, colPtr, rowPtr, cP); cout << cP << endl; PrintBoard(board);

//White Player Turn. do { cout << "White Players Turn. Please give coordinates:(columns, row)" << endl; cP = 'w'; GetMove(&colPtr, &rowPtr); if (colPtr == -1 && rowPtr == -1) { token++; cout << "Player Passed Turn." << endl; } } while (!InBounds(rowPtr, colPtr) || !IsValidMove(board, rowPtr, colPtr)); board[rowPtr][colPtr] = -1; PrintBoard(board);

if (token == 2) { cout << "GAME OVER" << endl; GetValue(board); } }

headerfile---------------------

// Print the board to the screen. void PrintBoard(const OthelloMatrix &board);

// Returns true if row and col are in the bounds of the board. bool InBounds(int row, int col);

// Returns true if the row and column are in bounds of the board and the square // is empty, or if the row and column represent a "pass" move. Does not // make sure the move actually causes pieces to flip. bool IsValidMove(const OthelloMatrix &board, int row, int col);

// Uses cin to read a row and column from the user into the two pointers. void GetMove(int *row, int *col);

// Takes a board, a row and column, and a player, and applies that player's move // to that position, flipping pieces according to the game rules. void ApplyMove(OthelloMatrix &board, int row, int col, char currentPlayer);

// Returns the sum of the black pieces minus the sum of the white pieces. int GetValue(const OthelloMatrix &board);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

Students also viewed these Databases questions

Question

=+3. How will you measure action objective?

Answered: 1 week ago

Question

3. Who would the members be?

Answered: 1 week ago

Question

4. Who would lead the group?

Answered: 1 week ago