I have to code this in C++. I have the connect four part but not sure how to do
ID: 3719329 • Letter: I
Question
I have to code this in C++. I have the connect four part but not sure how to do reversi. It has to be an AI type program where the user plays the computer. It does not have to use a graphical interface, can be simple command line interface.
Write a program that plays “ConnectReversi” game. Here is the rule of ConnectReversi: 7x7 board (same as connect 4) Two players take turns to place a piece. No one can skip a turn. Players choose a column to place a piece. The piece is placed to top the last piece on the column. (same as connect 4) When a piece is placed, it turns all opponents pieces that are placed between the current piece and another piece by the same player into current player’s pieces. This applies to horizontal, vertical, and diagonal directions. There is an exception: when there are 3 or more opponent pieces, they can’t be turned. The game finishes when the board becomes full. The winner is the one who has more pieces on the board.
Explanation / Answer
Hi please find the answer
if you have any query please let me know first i will give resolution
============================================================================
C++ program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BOARD_ROWS 7
#define BOARD_COLS 7
// declare prototype
void printBoard(char *board);
int takeTurn(char *board, int player, const char*);
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);
// main function start here
int main(int argc, char *argv[]){
const char *PIECES = "XO";
char board[BOARD_ROWS * BOARD_COLS];
memset(board, ' ', BOARD_ROWS * BOARD_COLS);
int turn, done = 0;
for(turn = 0; turn < BOARD_ROWS * BOARD_COLS && !done; turn++){
printBoard(board);
while(!takeTurn(board, turn % 2, PIECES)){
printBoard(board);
puts("**Column full!** ");
}
done = checkWin(board);
}
printBoard(board);
if(turn == BOARD_ROWS * BOARD_COLS && !done){
puts("It's a tie!");
} else {
turn--;
printf("Player %d (%c) wins! ", turn % 2 + 1, PIECES[turn % 2]);
}
return 0;
}
// this function are use to draw a board
// and display all element are enter by player
void printBoard(char *board){
int row, col;
//system("clear");
puts(" ****** Connect Reversi ****** ");
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS; col++){
printf("| %c ", board[BOARD_COLS * row + col]);
}
puts("|");
puts("-----------------------------");
}
puts(" 1 2 3 4 5 6 7 ");
}
// this function take the turn of player
// and take input of player and display the data on board
int takeTurn(char *board, int player, const char *PIECES){
int row, col = 0;
printf("Player %d (%c): Enter number coordinate: ", player + 1, PIECES[player]);
while(1){
if(1 != scanf("%d", &col) || col < 1 || col > 7 ){
while(getchar() != ' ');
puts("Number out of bounds! Try again.");
} else {
break;
}
}
col--;
for(row = BOARD_ROWS - 1; row >= 0; row--){
if(board[BOARD_COLS * row + col] == ' '){
board[BOARD_COLS * row + col] = PIECES[player];
return 1;
}
}
return 0;
}
// this function check winner of game
int checkWin(char *board){
//return all the check function
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}
// this function is use to check matching four of same type element
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
// this function are use to check horizontal
int horizontalCheck(char *board){
int row, col, idx;
const int WIDTH = 1;
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS - 3; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3)){
return 1;
}
}
}
return 0;
}
// this function check the board vertical
// for matching four element
int verticalCheck(char *board){
int row, col, idx;
const int HEIGHT = 7;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3)){
return 1;
}
}
}
return 0;
}
// this function check the board diagonal
// for matching four element
int diagonalCheck(char *board){
int row, col, idx, count = 0;
const int DIAG_RGT = 8, DIAG_LFT = 8;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if(count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3) || count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3)){
return 1;
}
count++;
}
count = 0;
}
return 0;
}