Question: The following C++ code needs to be modified so that all the outputs is to a file named results.txt and nothing should output to the
The following C++ code needs to be modified so that all the outputs is to a file named results.txt and nothing should output to the console. Please help.
#include
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 void Gomoku::printBoard() { for (int i=0; i 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 //check every diagonal from top right to bottom left for (int i=0;i 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 bool Gomoku::checkRightToLeftDiagonal() { //check every diagonal from top right to bottom left for (int i=0;i } */ 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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
