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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
using std::cin;
#include "TicTacToeOO.h"
// constructeur de board
Board::Board()
{
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = noone;
}
}
}
// affiche les colones (private)
void Board::drawRow(unsigned int i) const
{
cout << " " << symbol[board[i][0]]
<< " | " << symbol[board[i][1]]
<< " | " << symbol[board[i][2]]
<< endl;
}
// print: affiche la board
// | |
// ---|---|---
// X | | O
// ---|---|---
// X | O |
void Board::print() const
{
drawRow(0); // ligne du haut
cout << "---|---|---" << endl;
drawRow(1); // ligne du milieu
cout << "---|---|---" << endl;
drawRow(2); // ligne du bas
cout << endl << endl;
}
// retourne la valeur d'un champ
int Board::getVal(unsigned int r, unsigned int c) const
{
if(r>2 || c>2 || c<0 || r<0)
{
return -1;
}
else return board[r][c];
}
// inscrit une valeur dans un champ
bool Board::setVal(unsigned int r, unsigned int c, player val)
{
if(getVal(r,c)==0)
{
board[r][c]=val;
return true;
}
else return false;
}
// hasWon: controle qui a gagné
player Board::getWinner() const
{
int i = 0;
while(i < 3) {
if(board[0][i] == board[1][i] == board[2][i]) {
return board[0][i];
} else if(board[i][0] == board[i][1] == board[i][2]) {
return board[i][0];
}
i++;
}
if(board[0][0] == board[1][1] == board[2][2]) {
return board[0][0];
} else if(board[0][2] == board[1][1] == board[2][0]) {
return board[0][2];
}
return noone;
}
// constructeur de player
Player::Player(player playerNumber)
{
if(playerNumber==1)
{
p=player1;
}
else if(playerNumber==2)
{
p=player2;
}
else p=noone;
}
// makeMove: fait jouer un coup à un joueur
void Player::makeMove(Board &b)
{
int r,c;
do {
r = rand() % 3;
c = rand() % 3;
} while(b.getVal(r,c)!= 0);
b.setVal(r,c,p);
}
// constructeur game
Game::Game(Player &pl1, Player &pl2)
{
p1=&pl1;
p2=&pl2;
}
// play: chaque joueur joue
Player* Game::play()
{
p1->makeMove(b);
p2->makeMove(b);
if((b.getWinner())==noone) return NULL;
if(b.getWinner()==player1) return p1;
if(b.getWinner()==player2); return p2;
}
int main()
{
// initialisation variables
srand(time(NULL));
Board myboard;
Player play0=Player(noone);
Player play1=Player(player1);
Player play2=Player(player2);
Game mygame=Game(play1, play2);
myboard.print();
// joue
int i=0;
while(i<9)
{
Player *mywin=mygame.play();
if(mywin==&play1)
{
myboard.print();
cout<<"player1 has won..."<<endl;
return 0;
}
else if(mywin==&play2)
{
myboard.print();
cout<<"player1 has won..."<<endl;
return 0;
}
else
{
myboard.print();
}
i++;
}
return 0;
} |
Partager