Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------
- name: Tyler Hann
- nsid: tch435
- lecture section : 05A
- lab section: T07
- student number: 11081310
- Assignment X
- Question Y
- Notes or comments to marker:
- ----------------------------------------------*/
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- //put additional include lines here as needed
- //include <packageName>
- using namespace std;
- void checkWin(char gameGrid[3][3])
- { int p1=0, p11=0, p111=0, p1111=0, p2=0, p22=0, p222=0, p2222=0, a=0, b=1, c=0;
- if (gameGrid[0][0]=='X')
- {
- p1++;
- }
- if (gameGrid[0][1]=='X')
- {
- p1++;
- }
- if (gameGrid[0][2]=='X')
- {
- p1++;
- }
- if (p1==3)
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[0][0]=='X')
- {
- p11++;
- }
- if (gameGrid[1][0]=='X')
- {
- p11++;
- }
- if (gameGrid[2][0]=='X')
- {
- p11++;
- }
- if (p11==3)
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[0][0]=='X')
- {
- p111++;
- }
- if (gameGrid[1][1]=='X')
- {
- p111++;
- }
- if (gameGrid[2][2]=='X')
- {
- p111++;
- }
- if (p111==3)
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[2][0]=='X')
- {
- p1111++;
- }
- if (gameGrid[1][1]=='X')
- {
- p1111++;
- }
- if (gameGrid[0][2]=='X')
- {
- p1111++;
- }
- if (p1111==3)
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[0][1]=='X' && gameGrid[1][1]=='X' && gameGrid[2][1]=='X')
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[0][2]=='X' && gameGrid[1][2]=='X' && gameGrid[2][2]=='X')
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[1][0]=='X' && gameGrid[1][1]=='X' && gameGrid[1][2]=='X')
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- if (gameGrid[2][0]=='X' && gameGrid[2][1]=='X' && gameGrid[2][2]=='X')
- {
- cout << "Player 1 wins!" << endl;
- a++;
- b=0;
- }
- /*------------------------------------------------------------*/
- if (gameGrid[0][0]=='O')
- {
- p2++;
- }
- if (gameGrid[0][1]=='O')
- {
- p2++;
- }
- if (gameGrid[0][2]=='O')
- {
- p2++;
- }
- if (p2==3)
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[0][0]=='O')
- {
- p22++;
- }
- if (gameGrid[1][0]=='O')
- {
- p22++;
- }
- if (gameGrid[2][0]=='O')
- {
- p22++;
- }
- if (p22==3)
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[0][0]=='O')
- {
- p222++;
- }
- if (gameGrid[1][1]=='O')
- {
- p222++;
- }
- if (gameGrid[2][2]=='O')
- {
- p222++;
- }
- if (p222==3)
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[2][0]=='O')
- {
- p2222++;
- }
- if (gameGrid[1][1]=='O')
- {
- p2222++;
- }
- if (gameGrid[0][2]=='O')
- {
- p2222++;
- }
- if (p2222==3)
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[0][1]=='O' && gameGrid[1][1]=='O' && gameGrid[2][1]=='O')
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[0][2]=='O' && gameGrid[1][2]=='O' && gameGrid[2][2]=='O')
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[1][0]=='O' && gameGrid[1][1]=='O' && gameGrid[1][2]=='O')
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- if (gameGrid[2][0]=='O' && gameGrid[2][1]=='O' && gameGrid[2][2]=='O')
- {
- cout << "Player 2 wins!" << endl;
- c++;
- b=0;
- }
- //Thought of grouping the if statements with &&s part way through as you can tell
- if (a!=0 && c!=0)
- {
- cout << "Both players technically won, therefore it is a draw." << endl;
- cout << "In real tic tac toe this is an impossible scenario." << endl;
- }
- if (a>1)
- {
- cout << "Player 1 has managed to score multiple times in the same game!" << endl;
- }
- if (c>1)
- {
- cout << "Player 2 has managed to score multiple times in the same game!" << endl;
- }
- if (b==1)
- {
- cout << "It is a draw!" << endl;
- }
- return;
- }
- // display the given array
- void displayGrid(char game[3][3])
- {
- cout << game[0][0] << '|' << game[0][1] << '|' << game[0][2] << endl;
- cout << "-+-+-" << endl;
- cout << game[1][0] << '|' << game[1][1] << '|' << game[1][2] << endl;
- cout << "-+-+-" << endl;
- cout << game[2][0] << '|' << game[2][1] << '|' << game[2][2] << endl;
- checkWin(game);
- }
- int main()
- { int stopend;
- char gameGrid[3][3] = {{'O','O','O'}, // declare and initialize a 3x3 grid, an arbitrary position
- {'O','O','O'}, // you should change this to test your functions
- {'O','O','O'}}; // or let the user input some symbols
- char player1 = 'X';
- char player2 = 'O';
- cout << "The position is: " << endl;
- displayGrid(gameGrid);
- // use your function checkWin() here
- // tell the user if it is a win or not
- // if it is a win, say who won!
- // test with other posiible game positions
- cin >> stopend;
- // "stopend" is so the console doesn't immediately close after the program is finished.
- return 0;
- }
- // end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement