mainOthello.cpp 9.53 KB
Newer Older
Jason Riggs's avatar
Jason Riggs committed
1
#include <iostream> // For cin and cout 
Jason Riggs's avatar
change    
Jason Riggs committed
2
#include <utility>
Jason Riggs's avatar
change    
Jason Riggs committed
3
#include "Board.h"
Jason Riggs's avatar
Jason Riggs committed
4
#include "AIplayer.h"
Jason Riggs's avatar
change    
Jason Riggs committed
5
#include "Cell.h"
Jason Riggs's avatar
Jason Riggs committed
6
7
8
9
using namespace std;


int main() {
Jason Riggs's avatar
Jason Riggs committed
10
    int seed = 0; 
Jason Riggs's avatar
change    
Jason Riggs committed
11
12
    int turn = 1;
    int difficulty = 0;
Jason Riggs's avatar
change    
Jason Riggs committed
13
    int x, y;
Jason Riggs's avatar
Jason Riggs committed
14
    int aiX, aiY;
Jason Riggs's avatar
change    
Jason Riggs committed
15
    pair<int, int> AImove;
Jason Riggs's avatar
change    
Jason Riggs committed
16
    bool winner = false;
Jason Riggs's avatar
Jason Riggs committed
17
    
Jason Riggs's avatar
Jason Riggs committed
18
    // Creates AI
Jason Riggs's avatar
change    
Jason Riggs committed
19
    AIplayer AIturn;
Jason Riggs's avatar
Jason Riggs committed
20
    
Jason Riggs's avatar
change    
Jason Riggs committed
21
    // Header
Jason Riggs's avatar
change    
Jason Riggs committed
22
23
24
    cout << "Welcome to Othello!" << endl;
    cout << "the game of Reversi" << endl;
    cout << endl;
Jason Riggs's avatar
change    
Jason Riggs committed
25
26
    
    // ask for difficulty level
Jason Riggs's avatar
change    
Jason Riggs committed
27
28
    cout << "What difficulty would you like to play against:" << endl;
    cout << "1) Easy" << endl;
Jason Riggs's avatar
Jason Riggs committed
29
30
    cout << "2) Hard" << endl;
    cout << "3) 2 Player" << endl;
Jason Riggs's avatar
change    
Jason Riggs committed
31
32
    cout << "Enter your choice: ";
    cin >> difficulty;
Jason Riggs's avatar
change    
Jason Riggs committed
33
    
Jason Riggs's avatar
change    
Jason Riggs committed
34
35
36
37
38
39
40
41
    // Error checking for difficulty
    while(cin.fail()){
          
        cin.clear();
        cin.ignore(100, '\n');
        cout << "Invalid selection."<<endl;
        cout << "What difficulty would you like to play against:" << endl;
        cout << "1) Easy" << endl;
Jason Riggs's avatar
Jason Riggs committed
42
43
        cout << "2) Hard" << endl;
        cout << "3) 2 Player" << endl;
Jason Riggs's avatar
change    
Jason Riggs committed
44
45
46
47
        cout << "Enter your choice: ";
        cin >> difficulty;
        
    }
Jason Riggs's avatar
change    
Jason Riggs committed
48
49
    
    // start the game
Jason Riggs's avatar
change    
Jason Riggs committed
50
    displayBoard board;// Brand new board
Jason Riggs's avatar
change    
Jason Riggs committed
51
    
Jason Riggs's avatar
change    
Jason Riggs committed
52
    board.start();
Jason Riggs's avatar
change    
Jason Riggs committed
53
    board.printBoard();
Jason Riggs's avatar
change    
Jason Riggs committed
54
    Cell check;
Jason Riggs's avatar
change    
Jason Riggs committed
55
    cout << "X goes first."<<endl;
Jason Riggs's avatar
Jason Riggs committed
56
57
58
59
    
    
    // 
    switch (difficulty) {
Jason Riggs's avatar
change    
Jason Riggs committed
60
        // Easy difficulty
Jason Riggs's avatar
Jason Riggs committed
61
        case 1: {
Jason Riggs's avatar
change    
Jason Riggs committed
62
63
64
65
66
67
68
            do {
                // Player one's move
                if (turn == 1) {
                    // Ask for players move
                    cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
                    cin >> x >> y;
                    // Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
69
                    if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
Jason Riggs's avatar
change    
Jason Riggs committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
                        // Player moves
                        board.playerMove1(x,y);
                        // Check for flips in every direction
                        board.upCheck(x, y, 'X', true);
                        board.downCheck(x, y, 'X', true);
                        board.leftCheck(x, y, 'X', true);
                        board.rightCheck(x, y, 'X', true);
                        
                        board.uprightCheck(x, y,'X', true);
                        board.upleftCheck(x, y,'X', true);
                        board.downrightCheck(x, y,'X', true);
                        board.downleftCheck(x, y,'X', true);
        
                        // Change to player 2
                        turn++;
                        
                    }else{
                        cout << "Invalid Move!"<<endl;
                        
                    }
                    
                    // Player two's Move
                } else if (turn == 2) {
Jason Riggs's avatar
change    
Jason Riggs committed
93
94
95
96
                    AImove = AIturn.move1(board);
                    aiX = AImove.first;
                    aiY = AImove.second;
                    //Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
97
98
99
100
101
102
103
104
                    if(check.isLegalPlacementfor(aiX, aiY, board.board, 'O', 'X')){
                        // Player moves
                        board.playerMove2(aiX,aiY);
                        // Check for flips in every direction
                        board.upCheck(aiX,aiY, 'O', true);
                        board.downCheck(aiX,aiY, 'O', true);
                        board.leftCheck(aiX,aiY, 'O', true);
                        board.rightCheck(aiX,aiY, 'O', true);
Jason Riggs's avatar
change    
Jason Riggs committed
105
                        
Jason Riggs's avatar
change    
Jason Riggs committed
106
107
108
109
                        board.uprightCheck(aiX,aiY,'O', true);
                        board.upleftCheck(aiX,aiY,'O', true);
                        board.downrightCheck(aiX,aiY,'O', true);
                        board.downleftCheck(aiX,aiY,'O', true);
Jason Riggs's avatar
change    
Jason Riggs committed
110
        
Jason Riggs's avatar
change    
Jason Riggs committed
111
                        //Change to player 1
Jason Riggs's avatar
change    
Jason Riggs committed
112
113
                        turn--;
                        
Jason Riggs's avatar
change    
Jason Riggs committed
114
115
116
                    }else{
                        // Prints invalid and then asks for another move
                        cout << "Invalid Move!"<<endl;
Jason Riggs's avatar
change    
Jason Riggs committed
117
                        
Jason Riggs's avatar
change    
Jason Riggs committed
118
                    }
Jason Riggs's avatar
change    
Jason Riggs committed
119
120
121
122
123
                    
                }
                board.printBoard();
                
            } while (winner == false);
Jason Riggs's avatar
change    
Jason Riggs committed
124
            
Jason Riggs's avatar
Jason Riggs committed
125
126
        }
            break;
Jason Riggs's avatar
change    
Jason Riggs committed
127
        // Hard difficulty
Jason Riggs's avatar
Jason Riggs committed
128
129
130
131
132
133
134
135
        case 2: {
            do {
                // Player one's move
                if (turn == 1) {
                    // Ask for players move
                    cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
                    cin >> x >> y;
                    // Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
136
                    if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
Jason Riggs's avatar
Jason Riggs committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                        // Player moves
                        board.playerMove1(x,y);
                        // Check for flips in every direction
                        board.upCheck(x, y, 'X', true);
                        board.downCheck(x, y, 'X', true);
                        board.leftCheck(x, y, 'X', true);
                        board.rightCheck(x, y, 'X', true);
                        
                        board.uprightCheck(x, y,'X', true);
                        board.upleftCheck(x, y,'X', true);
                        board.downrightCheck(x, y,'X', true);
                        board.downleftCheck(x, y,'X', true);
        
                        // Change to player 2
                        turn++;
                        
                    }else{
                        cout << "Invalid Move!"<<endl;
                        
                    }
                    
                    // Player two's Move
                } else if (turn == 2) {
Jason Riggs's avatar
change    
Jason Riggs committed
160
                    AImove = AIturn.move2(board);
Jason Riggs's avatar
change    
Jason Riggs committed
161
162
                    aiX = AImove.first;
                    aiY = AImove.second;
Jason Riggs's avatar
Jason Riggs committed
163
                    // Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
164
                    if(check.isLegalPlacementfor(aiX, aiY, board.board, 'O', 'X')){
Jason Riggs's avatar
change    
Jason Riggs committed
165
166
167
168
169
170
171
                        // Player moves
                        board.playerMove2(aiX,aiY);
                        // Check for flips in every direction
                        board.upCheck(aiX,aiY, 'O', true);
                        board.downCheck(aiX,aiY, 'O', true);
                        board.leftCheck(aiX,aiY, 'O', true);
                        board.rightCheck(aiX,aiY, 'O', true);
Jason Riggs's avatar
Jason Riggs committed
172
                        
Jason Riggs's avatar
change    
Jason Riggs committed
173
174
175
176
                        board.uprightCheck(aiX,aiY,'O', true);
                        board.upleftCheck(aiX,aiY,'O', true);
                        board.downrightCheck(aiX,aiY,'O', true);
                        board.downleftCheck(aiX,aiY,'O', true);
Jason Riggs's avatar
Jason Riggs committed
177
178
179
180
        
                        // Change to player 1
                        turn--;
                        
Jason Riggs's avatar
change    
Jason Riggs committed
181
182
183
                    }else{
                        // Prints invalid and then asks for another move
                        cout << "Invalid Move!"<<endl;
Jason Riggs's avatar
Jason Riggs committed
184
                        
Jason Riggs's avatar
change    
Jason Riggs committed
185
                    }
Jason Riggs's avatar
Jason Riggs committed
186
187
188
                    
                }
                board.printBoard();
Jason Riggs's avatar
change    
Jason Riggs committed
189
                
Jason Riggs's avatar
Jason Riggs committed
190
191
192
193
            } while (winner == false);
            
        }
            break;
Jason Riggs's avatar
change    
Jason Riggs committed
194
        // Two player
Jason Riggs's avatar
Jason Riggs committed
195
196
197
198
199
        case 3: {
            do {
                // Ask for players move
                cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
                cin >> x >> y;
Jason Riggs's avatar
change    
Jason Riggs committed
200
                
Jason Riggs's avatar
Jason Riggs committed
201
202
203
                // Player one's move
                if (turn == 1) {
                    // Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
204
                    if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
Jason Riggs's avatar
Jason Riggs committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                        // Player moves
                        board.playerMove1(x,y);
                        // Check for flips in every direction
                        board.upCheck(x, y, 'X', true);
                        board.downCheck(x, y, 'X', true);
                        board.leftCheck(x, y, 'X', true);
                        board.rightCheck(x, y, 'X', true);
                        
                        board.uprightCheck(x, y,'X', true);
                        board.upleftCheck(x, y,'X', true);
                        board.downrightCheck(x, y,'X', true);
                        board.downleftCheck(x, y,'X', true);
        
                        // Change to player 2
                        turn++;
                        
                    }else{
                        cout << "Invalid Move!"<<endl;
                        
                    }
                    
                    // Player two's Move
                } else if (turn == 2) {
                    // Check fo valid move
Jason Riggs's avatar
change    
Jason Riggs committed
229
                    if(check.isLegalPlacementfor(x, y, board.board, 'O', 'X')){
Jason Riggs's avatar
Jason Riggs committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
                        // Player moves
                        board.playerMove2(x,y);
                        // Check for flips in every direction
                        board.upCheck(x, y, 'O', true);
                        board.downCheck(x, y, 'O', true);
                        board.leftCheck(x, y, 'O', true);
                        board.rightCheck(x, y, 'O', true);
                        
                        board.uprightCheck(x,y,'O', true);
                        board.upleftCheck(x,y,'O', true);
                        board.downrightCheck(x,y,'O', true);
                        board.downleftCheck(x,y,'O', true);
        
                        // Change to player 1
                        turn--;
                        
                    }else{
                        // Prints invalid and then asks for another move
                        cout << "Invalid Move!"<<endl;
                        
                    }
                }
                board.printBoard();
Jason Riggs's avatar
change    
Jason Riggs committed
253
                
Jason Riggs's avatar
Jason Riggs committed
254
            } while (winner == false); 
Jason Riggs's avatar
change    
Jason Riggs committed
255
        }
Jason Riggs's avatar
Jason Riggs committed
256
            break;
Jason Riggs's avatar
change    
Jason Riggs committed
257
        
Jason Riggs's avatar
Jason Riggs committed
258
    }
Jason Riggs's avatar
change    
Jason Riggs committed
259
 
Jason Riggs's avatar
Jason Riggs committed
260
    
Jason Riggs's avatar
Jason Riggs committed
261
    
Jason Riggs's avatar
Jason Riggs committed
262
263
    return 0;
}