My C++ code produces can produce this output: But it needs to produce this outpu
ID: 3708078 • Letter: M
Question
My C++ code produces can produce this output:
But it needs to produce this output:
Please help me change it so it does this because I do not know how i can do this. Here is my code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
#include <ctime>
using namespace std;
class Gomoku
{
public:
//USER INTERFACE:
Gomoku(int);
void setBoard();
void printBoard();
bool determineWinner();
void moves();
bool validateMove();
void play();
private:
char **board;
int counter;
int row;
int column;
char ch;
string algorithm;
int boardSize;
int randomRow;
int randomColumn;
int rowCheck;
int columnCheck;
int counting;
int i1;
int j1;
int m1;
int i2;
int j2;
int m2;
int i3;
int j3;
int m3;
int i4;
int j4;
int m4;
};
//constructor
Gomoku::Gomoku(int boardSize)
{
// create board of size nxn
this->board = (char **)malloc(boardSize * sizeof(char *));
int i;
for( i = 0 ; i < boardSize ; i++ )
this->board[i] = (char *)malloc(boardSize * sizeof(char));
this->boardSize = boardSize;
setBoard();
}
void Gomoku::setBoard()
{
counter=0;
for (int i=0; i<boardSize;i++)
{
for (int j=0; j<boardSize;j++)
{
board[i][j] = 0;
}
}
}
void Gomoku::printBoard()
{
for (int i=0; i<boardSize;i++)
{
for (int j=0; j<boardSize; j++)
{
cout << board[i][j];
}
cout << endl;
}
}
void Gomoku::moves()
{
if (counter%2==0)
{
cout << "Player" << counter%2 << endl;
srand(time(0));
randomRow = rand()%boardSize;
randomColumn = rand()%boardSize;
row = randomRow;
column = randomColumn;
}
else
{
cout << "Player" << counter%2 << endl;
srand(time(0));
randomRow = rand()%boardSize;
randomColumn = rand()%boardSize;
row = randomRow;
column = randomColumn;
cout << "DUMB MOVE" << endl;
}
}
bool Gomoku::validateMove()
{
if ((row<0||row>boardSize-1)||(column<0||column>boardSize-1)||(board[row][column]!=0))
{
cout << "Invalid entry" << row << " " << column << endl;
return false;
}
else
{
return true;
cout << "Valid" << endl;
}
}
bool Gomoku::determineWinner()
{
if (counter%2==0)
{
ch = 'X';
}
else
{
ch = 'O';
}
if (validateMove())
{
board[row][column] = ch;
if (counter%2==0)
{
algorithm = "alg1";
}
else
{
algorithm = "alg2";
}
cout << "r" << row << "c" << column << " " << algorithm << endl;
counter ++;
}
int i, j, c;
for (i = 0; i < boardSize; i++)
{
c = 0;
for (j = 0; j < boardSize; j++)
{
//check every row
if (board[i][j] == ch)
{
c++;
if (c == 5)
return true;
}
else
{
c=0;
}
}
}
for (i = 0; i < boardSize; i++)
{
c = 0;
for (j = 0; j < boardSize; j++)
{
//check every column
if (board[j][i] == ch)
{
c++;
if (c == 5)
return true;
}
else
{
c=0;
}
}
}
//check every diagonal from top left to bottom right
for (int i=0;i<boardSize;i++)
{
for (int j=0;j<boardSize;j++)
{
if (board[i][j]==ch)
{
int c = 0;
for (int m=0;(i+m<boardSize)&&(j+m<boardSize);m++)
{
if (board[i+m][j+m]==ch)
{
c++;
if (c==5)
{
return true;
}
}
else
{
c=0;
}
}
}
}
}
//check every diagonal from top right to bottom left
for (int i=0;i<boardSize;i++)
{
for (int j=0;j<boardSize;j++)
{
if (board[i][j]==ch)
{
int c = 0;
for (int n=0;(i+n<boardSize)&&(j-n>=0);n++)
{
if (board[i+n][j-n]==ch)
{
c++;
if (c==5)
{
return true;
}
}
else
{
c=0;
}
}
}
}
}
if (counter==boardSize*boardSize)
{
cout << "Draw."<< endl;
printBoard();
//setBoard();
}
return false;
}
void Gomoku::play()
{
setBoard();
do
{
moves();
if (determineWinner())
{
if (counter%2==0)
{
cout << "win=alg2" << endl;
cout << endl;
printBoard();
}
else
{
cout << "win=alg1" << endl;
cout << endl;
printBoard();
}
break;
}
else
{
printBoard();
}
}
while (true);
}
int main()
{
int boardSize = 0;
ifstream inData;
inData.open("input.txt");
inData >> boardSize;
cout << "size=" << boardSize << endl;
Gomoku t(boardSize);
cout << "Gomoku" << endl;
cout << endl;
t.play();
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
#include <ctime>
using namespace std;
class Gomoku
{
public:
//USER INTERFACE:
Gomoku(int);
void setBoard();
void printBoard();
bool determineWinner();
void moves();
bool validateMove();
void play();
private:
char **board;
int counter;
int row;
int column;
char ch;
string algorithm;
int boardSize;
int randomRow;
int randomColumn;
int rowCheck;
int columnCheck;
int counting;
int i1;
int j1;
int m1;
int i2;
int j2;
int m2;
int i3;
int j3;
int m3;
int i4;
int j4;
int m4;
};
//constructor
Gomoku::Gomoku(int boardSize)
{
// create board of size nxn
this->board = (char **)malloc(boardSize * sizeof(char *));
int i;
for( i = 0 ; i < boardSize ; i++ )
this->board[i] = (char *)malloc(boardSize * sizeof(char));
this->boardSize = boardSize;
setBoard();
}
void Gomoku::setBoard()
{
counter=0;
for (int i=0; i<boardSize;i++)
{
for (int j=0; j<boardSize;j++)
{
board[i][j] = 0;
}
}
}
void Gomoku::printBoard()
{
for (int i=0; i<boardSize;i++)
{
for (int j=0; j<boardSize; j++)
{
cout << board[i][j];
}
cout << endl;
}
}
void Gomoku::moves()
{
if (counter%2==0)
{
cout << "Player" << counter%2 << endl;
srand(time(0));
randomRow = rand()%boardSize;
randomColumn = rand()%boardSize;
row = randomRow;
column = randomColumn;
}
else
{
cout << "Player" << counter%2 << endl;
srand(time(0));
randomRow = rand()%boardSize;
randomColumn = rand()%boardSize;
row = randomRow;
column = randomColumn;
cout << "DUMB MOVE" << endl;
}
}
bool Gomoku::validateMove()
{
if ((row<0||row>boardSize-1)||(column<0||column>boardSize-1)||(board[row][column]!=0))
{
cout << "Invalid entry" << row << " " << column << endl;
return false;
}
else
{
return true;
cout << "Valid" << endl;
}
}
bool Gomoku::determineWinner()
{
if (counter%2==0)
{
ch = 'X';
}
else
{
ch = 'O';
}
if (validateMove())
{
board[row][column] = ch;
if (counter%2==0)
{
algorithm = "alg1";
}
else
{
algorithm = "alg2";
}
cout << "r" << row << "c" << column << " " << algorithm << endl;
counter ++;
}
int i, j, c;
for (i = 0; i < boardSize; i++)
{
c = 0;
for (j = 0; j < boardSize; j++)
{
//check every row
if (board[i][j] == ch)
{
c++;
if (c == 5)
return true;
}
else
{
c=0;
}
}
}
for (i = 0; i < boardSize; i++)
{
c = 0;
for (j = 0; j < boardSize; j++)
{
//check every column
if (board[j][i] == ch)
{
c++;
if (c == 5)
return true;
}
else
{
c=0;
}
}
}
//check every diagonal from top left to bottom right
for (int i=0;i<boardSize;i++)
{
for (int j=0;j<boardSize;j++)
{
if (board[i][j]==ch)
{
int c = 0;
for (int m=0;(i+m<boardSize)&&(j+m<boardSize);m++)
{
if (board[i+m][j+m]==ch)
{
c++;
if (c==5)
{
return true;
}
}
else
{
c=0;
}
}
}
}
}
//check every diagonal from top right to bottom left
for (int i=0;i<boardSize;i++)
{
for (int j=0;j<boardSize;j++)
{
if (board[i][j]==ch)
{
int c = 0;
for (int n=0;(i+n<boardSize)&&(j-n>=0);n++)
{
if (board[i+n][j-n]==ch)
{
c++;
if (c==5)
{
return true;
}
}
else
{
c=0;
}
}
}
}
}
if (counter==boardSize*boardSize)
{
cout << "Draw."<< endl;
printBoard();
//setBoard();
}
return false;
}
void Gomoku::play()
{
setBoard();
do
{
moves();
if (determineWinner())
{
if (counter%2==0)
{
cout << "win=alg2" << endl;
cout << endl;
printBoard();
}
else
{
cout << "win=alg1" << endl;
cout << endl;
printBoard();
}
break;
}
else
{
printBoard();
}
}
while (true);
}
int main()
{
int boardSize = 0;
ifstream inData;
inData.open("input.txt");
inData >> boardSize;
cout << "size=" << boardSize << endl;
Gomoku t(boardSize);
cout << "Gomoku" << endl;
cout << endl;
t.play();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.