Need help seperating the following c++ code into seperate classes. Nothing else
ID: 3707315 • Letter: N
Question
Need help seperating the following c++ code into seperate classes. Nothing else should change. It currently only has 1 class. Here is the code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
using namespace std;
class Gomoku
{
public:
Gomoku(int);
void setBoard();
void printBoard();
bool checkEveryRow();
bool checkEveryColumn();
bool checkLeftToRightDiagonal();
bool checkRightToLeftDiagonal();
bool determineWinner();
void moves();
bool validateMove();
void play();
private:
char **board;
int counter;
int row;
int column;
char ch;
string algorithm;
int boardSize;
int rowChecker;
int columnChecker;
int winningCounter;
int diagonalChecker;
};
//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()
{
cout << "Player " << counter%2+1 << endl;
cout << "Enter row:" << endl;
cin >> row;
cout << "Enter column:" << endl;
cin >> column;
}
bool Gomoku::validateMove()
{
if ((row<1||row>boardSize)||(column<1||column>boardSize)||(board[row-1][column-1]!=0))
{
cout << "Invalid entry" << endl;
return false;
}
else
{
return true;
}
}
bool Gomoku::determineWinner()
{
if (counter%2==0)
{
ch = 'X';
}
else
{
ch = 'O';
}
if (validateMove())
{
board[row-1][column-1] = 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;
}
/*
bool Gomoku::checkEveryRow()
{
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;
}
}
}
}
bool Gomoku::checkEveryColumn()
{
int i, j, c;
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;
}
}
}
}
bool Gomoku::checkLeftToRightDiagonal()
{
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;
}
}
}
}
}
}
bool Gomoku::checkRightToLeftDiagonal()
{
//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;
}
}
}
}
}
}
*/
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
Hi,
Header File : Class declarations are stored in a separate file. A file that contains a class declaration is called header file. The name of the class is usually the same as the name of the class, with a .h extension.
1. Gomoku.h
#ifndef GOMOKU_H
#define GOMOKU_H
public:
Gomoku(int);
void setBoard();
void printBoard();
bool checkEveryRow();
bool checkEveryColumn();
bool checkLeftToRightDiagonal();
bool checkRightToLeftDiagonal();
bool determineWinner();
void moves();
bool validateMove();
void play();
private:
char **board;
int counter;
int row;
int column;
char ch;
string algorithm;
int boardSize;
int rowChecker;
int columnChecker;
int winningCounter;
int diagonalChecker;
};
#endif
Implementation File
The member function definitions for a class are stored in a separate .cpp file, which is called the class implementation file. The file usually has the same name as the class, with the .cpp extension.
2. Gomoku.cpp
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
#include "Gomoku.h"
using namespace std;
//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()
{
cout << "Player " << counter%2+1 << endl;
cout << "Enter row:" << endl;
cin >> row;
cout << "Enter column:" << endl;
cin >> column;
}
bool Gomoku::validateMove()
{
if ((row<1||row>boardSize)||(column<1||column>boardSize)||(board[row-1][column-1]!=0))
{
cout << "Invalid entry" << endl;
return false;
}
else
{
return true;
}
}
bool Gomoku::determineWinner()
{
if (counter%2==0)
{
ch = 'X';
}
else
{
ch = 'O';
}
if (validateMove())
{
board[row-1][column-1] = 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;
}
/*
bool Gomoku::checkEveryRow()
{
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;
}
}
}
}
bool Gomoku::checkEveryColumn()
{
int i, j, c;
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;
}
}
}
}
bool Gomoku::checkLeftToRightDiagonal()
{
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;
}
}
}
}
}
}
bool Gomoku::checkRightToLeftDiagonal()
{
//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;
}
}
}
}
}
}
*/
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);
}
Client Code
client code, is the one that includes the main function.
3. main.cpp
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
#include "Gomoku.h"
using namespace std;
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;
}
Let me know if any change requied.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.