Question
i need help fixing my code so the user is able to play a game of mancala against a CPU Write a C++ program where
i need help fixing my code so the user is able to play a game of mancala against a CPU
Write a C++ program where a human will play the computer in a game of Mankala. We will make the smart computer once we get this first part working.
Use simple characters to illustrate the board and letters to identify the pits, similar to that show below:
4 4 4 4 4 4
0 0
4 4 4 4 4 4
A B C D E F
code:
#include
using namespace std;
const int num_pits = 6; const int stones_per_pit = 4;
void print_board(int board[]) { cout << " "; for (int i = num_pits - 1; i >= 0; i--) cout << board[i] << " "; cout << endl << board[num_pits] << " " << board[2*num_pits+1] << endl; cout << " "; for (int i = 0; i < num_pits; i++) cout << board[i+num_pits+1] << " "; cout << endl << "A B C D E F" << endl; }
void move(int board[], int player, int pit) { int stones = board[pit]; board[pit] = 0; int i = pit; while (stones > 0) { i = (i + 1) % (2*num_pits+2); if (i == num_pits) i = (i + 1) % (2*num_pits+2); board[i]++; stones--; } if (i == player && board[i] == 1) { int opposite_pit = 2*num_pits - i; board[player] += board[opposite_pit] + 1; board[opposite_pit] = 0; board[player]--; } if (i != player || board[i] > 1) player = (player + 1) % (2*num_pits+2); print_board(board); if (player == num_pits) { cout << "Your turn: "; char c; cin >> c; int pit = c - 'A'; move(board, player, pit); } else { cout << "My turn..." << endl; } }
int main() { int board[2*num_pits+2]; for (int i = 0; i < 2*num_pits+2; i++) board[i] = (i == num_pits || i == 2*num_pits+1) ? 0 : stones_per_pit; print_board(board); move(board, num_pits, -1); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started