Code:
/main.cpp
#include <time.h>
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <string>
#include "human.h"
#include "computer.h"
#include "board.h"
int main()
{
UserInterface ui;//human player object
ComputerOpponent cp;//computer player object
Board bd;//board object
srand(time(NULL));//seed random number generator
int a, b, c, d, j, k;
bd.instructions();//print instructions
for (j=1,k=1; j<=3,k<=3; j++,k++)
{
do {
ui.turn(a);// human move
} while (!bd.validTurn(a));
bd.xturn(a);// store human move in array
bd.printBoard();// print board
bd.xStorePos(a,j);// store position for X (human)
do {
cp.turn(b);// computer move
} while (!bd.validTurn(b));
bd.oturn(b);// store computer move in array
bd.printBoard();// print board
bd.oStorePos(b,j);// store position for O (computer)
}// for
bd.sortXpos();// sorting X positions
bd.sortOpos();// sorting O positions
bd.printBoard();// print board
if (bd.threeInaRow() ) {
cout << "Three in a row! End of game\n\n" << endl;
exit(1);
}// if
// do {
do {
ui.getMove(a,b,j);// ask human to enter to & from positions
bd.printBoard();// print board
} while (!bd.xvalidMove(a,b,j));
bd.getXpos(a,b);// replaces x array with new position
bd.xapplyMove(a,b);// apply valid moves
bd.printBoard();// print board
bd.xStorePos(b,j);// store positions for X (human)
bd.sortXpos();// sorting X positions
if (bd.threeInaRow() ) {
cout << "\nThree in a row! End of game\n\n" << endl;
exit(1);
}// if
do {
cp.getMove(c,d,k);// ask computer to enter to & from positions
bd.printBoard();// print board
} while (!bd.ovalidMove(c,d,k));
// logical errors occuring in oStorepos or sortOpos (not sure) <------------
bd.getOpos(c,d);// replaces o array with new position
bd.oapplyMove(c,d);// apply valid moves
bd.printBoard();// print board
bd.oStorePos(d,k); // store position for O (computer)
bd.sortOpos();// sorting O positions
if (bd.threeInaRow() ) {
cout << "\nThree in a row! End of game\n\n" << endl;
exit(1);
}// if
// } while (!bd.threeInaRow());
return 0;
}
//board.cpp
#include "board.h"
#include "swap.h"
#include "computer.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
Board::Board() {
for (int x=1; x<10; x++)
{
board[x]='\0';
}
}// initialize board to null values
void Board::printBoard() {
cout << "\t " << board[1] << " " << board[2] << " " << board[3] << endl;
cout << "\t" << "---+---+---" << endl;
cout << "\t " << board[4] << " " << board[5] << " " << board[6] << endl;
cout << "\t" << "---+---+---" << endl;
cout << "\t " << board[7] << " " << board[8] << " " << board[9] << endl;
}// print board
void Board::xturn(int &move) {
board[move]='X';
}// print X's move on board
void Board::xStorePos(int &move, int j) {
x_position[j]=move;
cout << "\nxStorePos();" << endl;
cout << "index j = " << j << " x_position contents " << move << endl;
}// stores X's position in array x_position
void Board::oturn(int &move) {
board[move]='O';
}// print O's move on board
void Board::oStorePos(int &move,int k) {
o_position[k]=move;
cout << "\noStorePos();" << endl;
cout << "index k = " << k << " o_position contents " << move << "\n" << endl;
}// stores O's position in array o_position
void Board::sortXpos() {
int i=1;
if (x_position[i] > x_position[i+2]) swap (&x_position[i], &x_position[i+2]);
if (x_position[i+1] > x_position[i+2]) swap (&x_position[i+1],&x_position[i+2]);
if (x_position[i] > x_position[i+1]) swap (&x_position[i],&x_position[i+1]);
// cout statements for error checking
cout << "\n\n\n Printing x_position sorted array" << endl;
for (int x=1; x<=3; x++)
{
cout << "index x is " << x;
cout << "\tcontents of array position " << x_position[x] << endl;
}
}// sort X's positions
void Board::sortOpos() {
int j=1;
if (o_position[j] > o_position[j+2]) swap (&o_position[j], &o_position[j+2]);
if (o_position[j+1] > o_position[j+2]) swap (&o_position[j+1],&o_position[j+2]);
if (o_position[j] > o_position[j+1]) swap (&o_position[j],&o_position[j+1]);
//cout statements for error checking
cout << "\n\n\n Printing o_position sorted array" << endl;
for (int x=1; x<=3; x++) {
cout << "index x is " << x;
cout << "\tcontents of array position " << o_position[x] << endl;
}
}// sort X's positions
bool Board::threeInaRow() {
if ((x_position[1]==1 && x_position[2]==2 && x_position[3]==3)
|| (x_position[1]==4 && x_position[2]==5 && x_position[3]==6)
|| (x_position[1]==7 && x_position[2]==8 && x_position[3]==9)
|| (o_position[1]==1 && o_position[2]==2 && o_position[3]==3)
|| (o_position[1]==4 && o_position[2]==5 && o_position[3]==6)
|| (o_position[1]==7 && o_position[2]==8 && o_position[3]==9)
|| (x_position[1]==1 && x_position[2]==4 && x_position[3]==7)
|| (x_position[1]==2 && x_position[2]==5 && x_position[3]==8)
|| (x_position[1]==3 && x_position[2]==6 && x_position[3]==9)
|| (o_position[1]==1 && o_position[2]==4 && o_position[3]==7)
|| (o_position[1]==2 && o_position[2]==5 && o_position[3]==8)
|| (o_position[17]==3 && o_position[2]==6 && o_position[3]==9))
{
return true;
}
else {
return false;
}
}// threeInaRow
void Board::swap (int *v1, int *v2) {
int temp = *v1;
*v1 = *v2;
*v2 = temp;
}// swap function
void Board::xapplyMove(int& from, int& to) {
board[from] = '\0';
board[to] = 'X';
}// apply X's valid move
void Board::oapplyMove(int& from, int& to) {
board[from] = '\0';
board[to] = 'O';
}// apply O's valid move
void Board::instructions() {
cout << "\n\nThank you for playing Three Men's Morris" << endl;
cout << "\nHow to play: " << endl;
cout << "Players take turns putting one marker at a time in any unoccupied box." << endl;
cout << "One player is X, the other is O. There are only 6 markers." << endl;
cout << "If neither player has 3 in a row after all six markers have been placed," << endl;
cout << "play continues. The markers can be moved vertically or horizontally" << endl;
cout << "on intersection points during the game. The winner is the firsst" << endl;
cout << "player to get three of their markers in a row." << endl;
cout << "\nPlease enter corresponding numbers on the grid below to place your positions.\n" << endl;
cout << "\t " << "1" << " " << "2" << " " << "3" << endl;
cout << "\t" << "---+---+---" << endl;
cout << "\t " << "4" << " " << "5" << " " << "6" << endl;
cout << "\t" << "---+---+---" << endl;
cout << "\t " << "7" << " " << "8" << " " << "9" << endl;
cout << "\n\nGood Luck!" << endl;
}// instructions
void Board::getXpos(int& from, int& to) {
int i=1;
cout << "getXpos () " << endl; //for error checking
if (x_position[i]==from) {
x_position[i]=to;
cout << "x_position[i] is: " << x_position[i] << endl;
}
if (x_position[i+1]==from) {
x_position[i+1]=to;
cout << "x_position[i+1] is: " << x_position[i+1] << endl;
}
if (x_position[i+2]==from) {
x_position[i+2]=to;
cout << "x_position[i+2] is: " << x_position[i+2] << endl;
}
}
void Board::getOpos(int& from, int& to) {
int i=1;
cout << "getOpos () " << endl; //for error checking
if (o_position[i]==from) {
o_position[i]=to;
cout << "o_position[i] is: " << o_position[i] << endl;
}
if (o_position[i+1]==from) {
o_position[i+1]=to;
cout << "o_position[i+1] is: " << o_position[i+1] << endl;
}
if (o_position[i+2]==from) {
o_position[i+2]=to;
cout << "o_position[i+2] is: " << o_position[i+2] << endl;
}
}
bool Board::validTurn(int& move) {
if (board[move] == '\0') {
return true;
}
else {
cout << "\nInvalid move!" << endl;
return false;
}
}// check X & O for valid move
bool Board::xvalidMove(int& from, int& to, int j) {
if (board[from] == 'X' && board[to] == '\0') {
return true;
}
else {
cout << "Invalid move!" << endl;
return false;
}
}// check X for valid from/to move
bool Board::ovalidMove(int& from, int& to, int k) {
if (board[from] == 'O' && board[to] == '\0') {
return true;
}
else {
cout << "Invalid move!" << endl;
return false;
}
}// check O for valid from/to move
//computer.cpp
#include <stdlib.h>
#include <string>
#include <time.h>
#include "Computer.h"
#include "board.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void ComputerOpponent::getMove(int &from, int &to, int k) // computer player
{
cout << "\n\nComputer moving positions . . ." << endl;
cout << "Enter from: "<< endl;
cin >> from;
cout << "Enter to: " << endl;
cin >> to;
/* implement code below when logical errors are fixed
from=rand()% 9+1;
to=rand() % 9+1;
cout << "Attempting to move from position" << from << " to " << to << endl;
*/
}// get move
ComputerOpponent::ComputerOpponent() {
strcpy(name, "The Matrix");
}
void ComputerOpponent::setName(char *n) {
cout << "\n\n" << endl;
strcpy(name, n);
cout << name << " has started!" << endl;
}// set name
void ComputerOpponent::turn(int &move)
{
cout << "\nComputer enter position: ";
cin >> move;
/* implement code below when logical errors are fixed
move = rand() % 9+1;
cout << "\nThe computer moved here . . . " << move << "\n\n";
return move;
*/
}// get initial positions
//human.cpp
#include <iostream.h>
#include <string>
#include "human.h"
#include "board.h"
void UserInterface::getMove(int& from , int& to, int j) {
cout << "\n\nHuman enter moves please" << endl;
cout << "Enter from: "<< endl;
cin >> from;
cout << "Enter to: " << endl;
cin >> to;
} // get move
void UserInterface::turn(int &move) {
cout << "\nHuman enter position: ";
cin >> move;
}// get initial positions
///board.h
#ifndef _BOARD_H
#define _BOARD_H
class Board {
public:
Board();// constructor
void printBoard();// prints board
void xturn(int &move);// human turn (X)
void oturn(int &move);// computer turn (O)
void xStorePos(int &move, int pos); // store X position in array
void oStorePos(int &move, int pos);// store O position in aray
void sortXpos();// sort X array
void sortOpos();// sort O array
void swap(int *v1, int *v2);// swap function
void xapplyMove(int& from, int& to);// apply X's valid move
void oapplyMove(int& from, int& to);// apply O's valid move
void instructions();// print instructions
void getXpos(int& from, int& to);// get from/to positions from human
void getOpos(int& from, int& to);// get from/to positions from computer
bool threeInaRow();// check for winner
bool validTurn(int& move);// validity check for 1st three initial positions
bool xvalidMove(int& from, int& to, int pos);// validity check for X's from/to positions
bool ovalidMove(int& from, int& to, int pos);// validity check for O's from/to positions
private:
char board[10];
int x_position[3];
int o_position[3];
};
#endif
//swap.h
#ifndef _SWAP_H
#define _SWAP_H
void swap (int *, int *); // "swap.h"
#endif
// human.h
#ifndef _HUMAN_H
#define _HUMAN_H
class UserInterface {
public:
void getMove(int& from, int& to, int j);
void turn(int &move);
private:
char playerName[30];
};
#endif
//computer.h
#ifndef _COMPUTER_H
#define _COMPUTER_H
class ComputerOpponent {
public:
void getMove(int&from, int &to, int j);
int printMove(int&from, int &to);
char *getName() {return name;}
void setName(char *);
ComputerOpponent();
void turn(int &move);
void srand(unsigned int);
private:
char name[30];
};
#endif