• API
• FAQ
• Tools
• Archive
daily pastebin goal
1%
SHARE
TWEET

# Untitled

a guest Mar 19th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <vector>
2. #include <iostream>
3.
4. using namespace std;
5.
6. /// @brief Draws the provided tic-tac-toe board to the screen
7. //  @param board is the tic-tac-toe board that should be drawn
8. void drawBoard(const vector < char >&gameBoard) {
9.     for (int i = 0; i < 9; i += 3) {
10.         cout << "  " << gameBoard.at(i) << "  |  " << gameBoard.at(i + 1) << "  |  "
11.             << gameBoard.at(i + 2) << "  " << endl;
12.         if (i < 6) {
13.             cout << "-----|-----|-----" << endl;
14.         }
15.     }
16.     cout << endl;
17.
18.     return;
19. }
20.
21.
22.
23. /// @brief Fills vector with characters starting at lower case a.
24. ///
25. ///     If the vector is size 3 then it will have characters a to c.
26. ///     If the vector is size 5 then it will have characters a to e.
27. ///     If the vector is size 26 then it will have characters a to z.
28. ///
29. /// @param v the vector to initialize
30. /// @pre-condition the vector size will never be over 26
31. void initVector(vector <char> &v) {
32.
33.     char c = 'a';
34.
35.     for (int i = 0; i < 9; i++)
36.     {
37.         v.at(i) = c;
38.         c += 1;
39.     }
40.
41.     return;
42. }
43.
44.
45. /// @brief Converts a character representing a cell to associated vector index
46. /// @param the position to be converted to a vector index
47. /// @return the integer index in the vector, should be 0 to (vector size - 1)
48. int convertPosition(char boardPosition) {
49.
50.     if (boardPosition == 'a')
51.     {
52.         return 0;
53.     }
54.
55.     else if (boardPosition == 'b')
56.     {
57.         return 1;
58.     }
59.
60.     else if (boardPosition == 'c')
61.     {
62.         return 2;
63.     }
64.
65.     else if (boardPosition == 'd')
66.     {
67.         return 3;
68.     }
69.
70.     else if (boardPosition == 'e')
71.     {
72.         return 4;
73.     }
74.
75.     else if (boardPosition == 'f')
76.     {
77.         return 5;
78.     }
79.
80.     else if (boardPosition == 'g')
81.     {
82.         return 6;
83.     }
84.
85.     else if (boardPosition == 'h')
86.     {
87.         return 7;
88.     }
89.
90.     else if (boardPosition == 'i')
91.     {
92.         return 8;
93.     }
94.
95.     return -1;
96. }
97.
98.
99. /// @brief Predicate function to determine if a spot in board is available.
100. /// @param board the current tic-tac-toe board
101. /// @param position is an index into vector to check if available
102. /// @return true if position's state is available (not marked) AND is in bounds
103. bool validPlacement(const vector<char>  &gameBoard, int positionIndex) {
104.
105.     if (gameBoard.at(positionIndex) != 'X' && gameBoard.at(positionIndex) != 'O' && positionIndex < 9)
106.     {
107.         return true;
108.     }
109.
110.     return false;
111. }
112.
113. /// @brief Acquires a play from the user as to where to put her mark
114. ///
115. ///     Utilizes convertPosition and validPlacement functions to convert the
116. ///     user input and then determine if the converted input is a valid play.
117. ///
118. /// @param board the current tic-tac-toe board
119. /// @return an integer index in board vector of a chosen available board spot
120. int getPlay(const vector<char> &gameBoard) {
121.
122.     // TODO: implement function
123.     char boardPosition = ' ';
124.
125.     cout << "Please choose a position: " << endl;
126.     cin >> boardPosition;
127.
128.     if (validPlacement(gameBoard, convertPosition(boardPosition)) == false)
129.     {
130.         while (validPlacement(gameBoard, convertPosition(boardPosition)) == false)
131.         {
132.             cout << "Please choose a position: " << endl;
133.             cin >> boardPosition;
134.         }
135.     }
136.
137.     return convertPosition(boardPosition);
138.
139.     return -1;
140. }
141.
142.
143. /// @brief Predicate function to determine if the game has been won
144. ///
145. ///     Winning conditions in tic-tac-toe require three marks from same
146. ///     player in a single row, column or diagonal.
147. ///
148. /// @param board the current tic-tac-toe board
149. /// @return true if the game has been won, false otherwise
150. bool gameWon(const vector<char> &gameBoard) {
151.
152.     if(gameBoard.at(0) == 'X' && gameBoard.at(3) == 'X' && gameBoard.at(6) == 'X')
153.     {
154.         return true;
155.     }
156.
157.     else if(gameBoard.at(0) == 'O' && gameBoard.at(3) == 'O' && gameBoard.at(6) == 'O')
158.     {
159.         return true;
160.     }
161.
162.     else if(gameBoard.at(1) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(7) == 'X')
163.     {
164.         return true;
165.     }
166.
167.     else if(gameBoard.at(1) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(7) == 'O')
168.     {
169.         return true;
170.     }
171.
172.     else if(gameBoard.at(2) == 'X' && gameBoard.at(5) == 'X' && gameBoard.at(8) == 'X')
173.     {
174.         return true;
175.     }
176.
177.     else if(gameBoard.at(2) == 'O' && gameBoard.at(5) == 'O' && gameBoard.at(8) == 'O')
178.     {
179.         return true;
180.     }
181.
182.     else if(gameBoard.at(2) == 'X' && gameBoard.at(5) == 'X' && gameBoard.at(8) == 'X')
183.     {
184.         return true;
185.     }
186.
187.     else if(gameBoard.at(0) == 'X' && gameBoard.at(1) == 'X' && gameBoard.at(2) == 'X')
188.     {
189.         return true;
190.     }
191.
192.     else if(gameBoard.at(0) == 'O' && gameBoard.at(1) == 'O' && gameBoard.at(2) == 'O')
193.     {
194.         return true;
195.     }
196.
197.     else if(gameBoard.at(3) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(5) == 'X')
198.     {
199.         return true;
200.     }
201.
202.     else if(gameBoard.at(3) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(5) == 'O')
203.     {
204.         return true;
205.     }
206.
207.     else if(gameBoard.at(6) == 'X' && gameBoard.at(7) == 'X' && gameBoard.at(8) == 'X')
208.     {
209.         return true;
210.     }
211.
212.     else if(gameBoard.at(6) == 'O' && gameBoard.at(7) == 'O' && gameBoard.at(8) == 'O')
213.     {
214.         return true;
215.     }
216.
217.     else if(gameBoard.at(0) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(8) == 'X')
218.     {
219.         return true;
220.     }
221.
222.     else if(gameBoard.at(0) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(8) == 'O')
223.     {
224.         return true;
225.     }
226.
227.     else if(gameBoard.at(2) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(6) == 'X')
228.     {
229.         return true;
230.     }
231.
232.     else if(gameBoard.at(2) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(6) == 'O')
233.     {
234.         return true;
235.     }
236.     return false;
237. }
238.
239.
240. /// @brief Predicate function to determine if the board is full
241. /// @param board the current tic-tac-toe board
242. /// @return true iff the board is full (no cell is available)
243. bool boardFull(const vector<char> &gameBoard) {
244.
245.     // TODO: implement function
246.     int full = 0;
247.
248.     for (int i = 0; i < 9; i++)
249.     {
250.         if (gameBoard.at(i) == 'X' || gameBoard.at(i) == 'O')
251.         {
252.             full += 1;
253.         }
254.     }
255.
256.     if (full == 9)
257.     {
258.         return true;
259.     }
260.
261.     return false;
262. }
263.
264.
265. // Global constants for player representation
266. const int PLAYER1 = 0;
267. const int PLAYER2 = 1;
268.
269. int main() {
270.     // Variables that you may find useful to utilize
271.     vector<char> gameBoard(9);
272.     int curPlay;
273.     int whosTurn = PLAYER1; // Player 1 always goes first and is 'X'
274.
275.     /// TODO: Initialize board to empty state
276.     initVector(gameBoard);
277.     /// TODO: Display empty board
278.     drawBoard(gameBoard);
279.
280.     /// TODO: Play until game is over
281.     while(gameWon(gameBoard) == false || boardFull(gameBoard) == false)
282.     {
283.         /// TODO: Get a play
284.         if (whosTurn == PLAYER1)
285.         {
286.             curPlay = getPlay(gameBoard);
287.             gameBoard.at(curPlay) = 'X';
288.             whosTurn = PLAYER2;
289.             drawBoard(gameBoard);
290.             if (gameWon(gameBoard) == true)
291.             {
292.                 break;
293.             }
294.         }
295.
296.         else if (whosTurn == PLAYER2)
297.         {
298.             curPlay = getPlay(gameBoard);
299.             gameBoard.at(curPlay) = 'O';
300.             whosTurn = PLAYER1;
301.             drawBoard(gameBoard);
302.             if (gameWon(gameBoard) == true)
303.             {
304.                 break;
305.             }
306.         }
307.         /// TODO: Set the play on the board
308.
309.         /// TODO: Switch the turn to the other player
310.
311.         /// TODO: Output the updated board
312.     }
313.
314.     /// TODO: Determine winner and output appropriate message
315.     if (whosTurn == PLAYER1)
316.    {
317.        cout << "PLAYER 2 WINS!" << endl;
318.    }
319.
320.    else if (whosTurn == PLAYER2)
321.    {
322.        cout << "PLAYER 1 WINS!" << endl;
323.    }
324.
325.    else
326.    {
327.        cout << "No one wins" << endl;
328.    }
329.
330.     return 0;
331. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top