Well after my exams I have had trouble getting back into C++ so I thought I would start with something easy like Tic Tac Toe.
Header File:
Code:
void drawboard () {
cout << board[0][0] << '|' << board[0][1] << '|' << board[0][2] << endl;
cout << "-----" << endl;
cout << board[1][0] << '|' << board[1][1] << '|' << board[1][2] << endl;
cout << "-----" << endl;
cout << board[2][0] << '|' << board[2][1] << '|' << board[2][2] << endl;
cout << endl;
}
char turn () {
int temp = pturn % 2;
char player;
if (temp != 0) {
player = 'O';
}
else {
player = 'X';
}
return player;
}
void move () {
int row;
int col;
do {
cout << "Player " << pchar << "'s turn" << endl << endl;
cout << "Please enter the row number: ";
cin >> row;
cout << "Please enter the column number: ";
cin >> col;
--row;
--col;
if (!usedsq[row][col]) {
break;
}
cout << endl << "Sorry that square is taken" << endl;
cin.ignore();
cin.get();
system("CLS");
drawboard();
} while (1);
board[row][col] = pchar;
usedsq[row][col] = true;
system("CLS");
}
void win() {
if (board[0][0] == 'X' && board[0][1] == 'X' &&
board[0][2] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[1][0] == 'X' && board[1][1] == 'X' &&
board[1][2] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[2][0] == 'X' && board[2][1] == 'X' &&
board[2][2] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[0][0] == 'X' && board[1][1] == 'X' &&
board[2][2] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[0][2] == 'X' && board[1][1] == 'X' &&
board[2][0] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[0][0] == 'X' && board[1][0] == 'X' &&
board[2][0] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[0][1] == 'X' && board[1][1] == 'X' &&
board[2][1] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
else if (board[0][2] == 'X' && board[1][2] == 'X' &&
board[2][2] == 'X') {
cout << "Congratulations Player X has won!" << endl;
pwin = true;
}
if (board[0][0] == 'O' && board[0][1] == 'O' &&
board[0][2] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[1][0] == 'O' && board[1][1] == 'O' &&
board[1][2] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[2][0] == 'O' && board[2][1] == 'O' &&
board[2][2] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[0][0] == 'O' && board[1][1] == 'O' &&
board[2][2] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[0][2] == 'O' && board[1][1] == 'O' &&
board[2][0] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[0][0] == 'O' && board[1][0] == 'O' &&
board[2][0] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[0][1] == 'O' && board[1][1] == 'O' &&
board[2][1] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
else if (board[0][2] == 'O' && board[1][2] == 'O' &&
board[2][2] == 'O') {
cout << "Congratulations Player O has won!" << endl;
pwin = true;
}
}
*That is a very long conditional statement I have there, wasn't sure how to cut it down*
Main:
Code:
#include <iostream>
#include <cstdlib>
#include "TicHead.h"
using namespace std;
int main()
{
int menuselect;
char end = 'N';
int noturns = 0;
cout << endl << "Welcome to Bumfluff's Tic Tac Toe" << endl;
cout << endl << "\t1. Start Game" << endl;
cout << "\t2. Instructions" << endl;
cout << "\t3. Quit" << endl;
cout << endl << "Please make a selection: ";
cin >> menuselect;
system("CLS");
switch (menuselect) {
case 1:
break;
case 2:
cout << "This is a two player game." << endl;
cout << "Type the number of the square" <<endl;
cout << "and press enter to put your character" << endl;
cout << "into your chosen square. You choose" << endl;
cout << "the square by selecting the column and row." << endl;
cout << endl << "The first person to get three in a row WINS!" << endl;
cout << endl << "Press [ENTER] To Start Game: ";
cin.ignore();
cin.get();
break;
case 3:
return 0;
default:
return -1;
}
do {
pturn = 0;
noturns = 0;
do {
system("CLS");
drawboard();
pchar = turn();
move();
win();
if (pwin) break;
++pturn;
if (noturns == 8) {
cout << "You have drawn" << endl;
break;
}
++noturns;
} while (1);
cout << endl << "Would you like to play again? (Y/N): ";
cin >> end;
if (end == 'N' || end == 'n') break;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++){
board[i][j] = 0;
usedsq[i][j] = 0;
}
}
} while (1);
return 0;
}
*I know I have used system commands*
My question is how would I go about making this a 1 player vs. computer game ie. how I make the algorithms.