Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TicTacToeGame.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <Windows.h> ///////////////////////////////////
- #include <iostream> // Tic-Tac-Toe //
- #include <cmath> // //
- #include <iomanip> // by 3nriched //
- #include <string> ///////////////////////////////////
- using namespace std; //declaration of std
- void display(char [][3]); /////////////////////
- int winCheck(char [][3], int); // Prototypes //
- void move(char [][3], int); /////////////////////
- char arraytest[3][3] = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}}; //the gameboard 2D array
- int playa = 0, //starts at player 0 but really its 1 dont change
- winner = 0, //holds the variable 1 or 2 when winner is determined
- flag = 0; //variable used with tie game checker
- int _tmain(int argc, _TCHAR* argv[])
- {
- system("Color 17"); //sets background to blue and text to white
- PlaySound("Pinkythebrain.wav", NULL, SND_ASYNC); //plays pinky and the brain song 1minute long
- do
- {
- if(playa == 1){playa = 2;}else playa = 1; //cycles through players 1 and 2
- display(arraytest); //displays the current gameboard
- winner = winCheck(arraytest, winner); //determines if a win has happened
- if(winner != 0){cout<<"\n";cout << "Player "<<winner<<" Wins!!" << endl;cout<<"\n";system("pause");return 0;} //displays the winner (if any)
- move(arraytest, playa); //handles the inputted position for players
- flag += 1;
- if(flag == 9){cout<<"\n";cout<<"The Game Has Tied!!\n";system("pause");return 0;} //displays tied game message
- }
- while(winner != 1 || winner != 2); //continues playing until a winner has been found
- }
- void display(char gameBoard[][3])
- {
- cout<<"\t\tTic-Tac-Toe\tPlayer: "<<playa<<endl; ///////////////
- for (int sixt=0;sixt<3;sixt++) { // Creates //
- for(int e=0;e<3;e++) { // Game //
- cout << gameBoard[sixt][e]; // Board //
- cout << " "; ///////////////
- }
- cout << endl;
- if(sixt!=2) {
- cout << "-------" << endl;
- }
- }
- }
- int winCheck(char gameBoard[][3], int winner) //win function - determines if a win occured.
- {
- //tests all rows for both X and O for a winner.
- if(gameBoard[0][0]==gameBoard[0][1] && gameBoard[0][0]==gameBoard[0][2]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
- if(gameBoard[1][0]==gameBoard[1][1] && gameBoard[1][0]==gameBoard[1][2]){if(gameBoard[1][0]=='X'){winner=1;}if(gameBoard[1][0]=='O'){winner=2;}}
- if(gameBoard[2][0]==gameBoard[2][1] && gameBoard[2][0]==gameBoard[2][2]){if(gameBoard[2][0]=='X'){winner=1;}if(gameBoard[2][0]=='O'){winner=2;}}
- //tests all colums for both X and O for a winner.
- if(gameBoard[0][0]==gameBoard[1][0] && gameBoard[0][0]==gameBoard[2][0]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
- if(gameBoard[0][1]==gameBoard[1][1] && gameBoard[0][1]==gameBoard[1][2]){if(gameBoard[0][1]=='X'){winner=1;}if(gameBoard[0][1]=='O'){winner=2;}}
- if(gameBoard[0][2]==gameBoard[1][2] && gameBoard[0][2]==gameBoard[2][2]){if(gameBoard[0][2]=='X'){winner=1;}if(gameBoard[0][2]=='O'){winner=2;}}
- //tests left top to bottom right diangle for both X and O for a winner.
- if(gameBoard[0][0]==gameBoard[1][1] && gameBoard[0][0]==gameBoard[2][2]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
- //tests right top to bottom left diangle for both X and O for a winner.
- if(gameBoard[0][2]==gameBoard[1][1] && gameBoard[0][2]==gameBoard[2][0]){if(gameBoard[0][2]=='X'){winner=1;}if(gameBoard[0][2]=='O'){winner=2;}}
- return winner; //returns the winner, if any is determined.
- }
- void move(char gameBoard[][3], int playa) // handles the position inputed by user
- {
- int x,y;
- cout << "PLAYER "<<playa<<" ENTER ROW POSITION(0,1,2, LEFT - RIGHT): " << endl; //requests player row input
- cin >> x;
- cout << "PLAYER "<<playa<<" ENTER COLUM POSITION(0,1,2, UP - DOWN): " << endl; //requests player colum input
- cin >> y;
- if(playa == 1) {gameBoard[x][y]='X';} //assigns X to player1 and O to player2
- if(playa == 2) {gameBoard[x][y]='O';}
- system("cls"); //refreshes screen
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement