1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//Implementaion file that includes all of the classes and their innards
//classImp.h
#include <iostream>
#include <cstdlib> //for random number generator
#include <ctime> //for seed (srand)
#include "dec.h"
#define LINES "\n\n\n\n\n\n\n\n\n\n\n\n"
#define TAB " "
#define BIGTAB "\t\t\t"
using namespace std;
void Draw::opponent(int &opp)
{
cout << "Who do you want to play against?\nEnter the number corresponding to your choice.\n";
cout << "1. Player\n";
cout << "2. Computer\n";
cin >> opp;
//discards characters until new line is found
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl;
//if user did not enter a valid repsonse
if (opp != 1 && opp != 2)
{
cout << "Please choose a valid choice.\n";
opponent(opp);
}
player = 0;
//playing against another player
if (opp == 1)
p_v_p(player1, player2);
//playing against the computer
else
p_v_comp(player1);
}
void Draw::p_v_p(string &player1, string &player2)
{
cout << "Player 1 name: ";
getline(cin, player1);
cout << "Player 2 name: ";
getline(cin, player2);
cout << endl;
cout << "To choose a square, type in the corresponding number.\n";
}
void Draw::p_v_comp(string &player1)
{
cout << "Player name: ";
getline(cin, player1);
cout << "To choose a square, type in the corresponding number.\n";
srand(time(NULL)); //For the random number generator
}
void Draw::cells()
{
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
//Draws the empty playing board
int Draw::drawBoard()
{
cout << LINES;
cout << BIGTAB << TAB << board[0][0] << " |" << TAB << board[0][1] << " |" << TAB << board[0][2] << "\n";
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
cout << BIGTAB << " " << "----+-----+----\n";
cout << BIGTAB << TAB << board[1][0] << " |" << TAB << board[1][1] << " |" << TAB << board[1][2] << "\n";
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
cout << BIGTAB << " " << "----+-----+----\n";
cout << BIGTAB << TAB << board[2][0] << " |" << TAB << board[2][1] << " |" << TAB << board[2][2] << "\n";
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
cout << BIGTAB << TAB << " |" << TAB << " |" << endl;
//next person's turn
if (player == 0 || player == 2)
player = 1;
else
player = 2;
int win = testWinner();
//if there is a winner or tie, exit the program
if (win == 1)
{
cout << "RETURN\n";
return 0;
}
if (win == 0)
{
//if playing against another player
if (opp == 1)
playerMoves(player1, player2, player, opp);
//if playing against computer
else
{
success = compMoves(player1, player);
if (success == 1)
return 0;
}
}
}
void Draw::playerMoves(string player1, string player2, int player, int opp)
{
if (player == 0 || player == 1)
cout << player1 << "'s turn: ";
else
{
if (opp == 1)
cout << player2 << "'s turn: ";
//if playing against computer, move to compMoves
else
compMoves(player1, player);
}
string input; //each player's move
getline(cin, input);
while (input != "1" && input != "2" && input != "3" && input != "4" && input != "5" &&
input != "6" && input != "7" && input != "8" && input != "9")
{
cout << "Error: Please enter a cell 1-9\n";
cout << "Please choose again: ";
getline(cin, input);
}
//makes the letter a character
char move = input[0];
//if the number is different, move on
if (!isSame(move))
newBoard(move, player);
//if it is the same, print revert back to ask for another number
else
playerMoves(player1, player2, player, opp);
}
int Draw::compMoves(string player1, int player)
{
opp = 2;
//defaults to playerMoves
if (player == 0 || player == 1)
playerMoves(player1, player2, player, opp);
else if (count == 9)
return 1;
else
{
//if it's the computer's turn
//if there are no possible winners
if (!two_in_row())
{
char move = randomCell();
newBoard(move, player);
}
}
}
|