-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (103 loc) · 2.3 KB
/
main.cpp
File metadata and controls
130 lines (103 loc) · 2.3 KB
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
#include <iostream>
#include "players.h"
#include "game.h"
using namespace std;
Player* getPlayer()
{
static int playerNumber = 1;
int input;
for (input = 0; input != 1 && input != 2;)
{
cout << "Select Player " << playerNumber << " (human: 1, ai: 2) ";
cin >> input;
}
playerNumber++;
if (input == 1)
{
return new HumanPlayer;
}
else // input == 2
{
return new AiPlayer;
}
}
void PrintBoard(const Game &game)
{
cout << endl;
for (int y = 0; y < game.BoardHeight; y++)
{
cout << " | ";
for (int x = 0; x < game.BoardWidth; x++)
{
char boardChar;
switch (game(x, y))
{
case BoardField::Empty:
boardChar = ' ';
break;
case BoardField::Player1:
boardChar = 'O';
break;
case BoardField::Player2:
boardChar = 'X';
break;
}
cout << boardChar << ' ';
}
cout << "|\n";
}
cout << " ---";
for (int x = 0; x < game.BoardWidth; x++)
{
cout << "--";
}
cout << endl;
cout << " ";
for (int x = 0; x < game.BoardWidth; x++)
{
cout << " " << x + 1;
}
cout << endl << endl;
}
int main()
{
cout << "* * * * * * * * * * * * *\n";
cout << " Welcome to Connect 4!\n\n";
Player* player1 = getPlayer();
Player* player2 = getPlayer();
Game game(*player1, *player2);
while (game.isRunning())
{
PrintBoard(game);
int curPlayer;
switch (game.getState())
{
case GameState::TurnP1:
curPlayer = 1;
break;
case GameState::TurnP2:
curPlayer = 2;
break;
}
cout << "Turn for Player " << curPlayer << ": ";
game.nextTurn();
}
PrintBoard(game);
switch (game.getState())
{
case GameState::P1Won:
cout << "Player 1 won the game!";
break;
case GameState::P2Won:
cout << "Player 2 won the game!";
break;
case GameState::Draw:
cout << "Draw, no winner.";
break;
}
cout << endl;
delete player1;
delete player2;
system("pause");
return 0;
}