ShowCase: Tic-Tac-Toe
Apr 22, 2013 at 4:42am UTC
Hello everyone!
I just wanted to share my Tic-Tac-Toe plugin I just made.
I'm new to C++, and it's a lot of fun.
If you see some things that need to be improved, go ahead and tell me.
Also, this is fully working! :)
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
// TIC-TAC-TOE - made by: ZVP
#include <iostream>
using namespace std;
int main() {
char Square1('1' );
char Square2('2' );
char Square3('3' );
char Square4('4' );
char Square5('5' );
char Square6('6' );
char Square7('7' );
char Square8('8' );
char Square9('9' );
int Playerturn(1);
bool Gameover(true );
do {
cout<<Square1<<"|" <<Square2<<"|" <<Square3<<endl;
cout<<"-+-+-" <<endl;
cout<<Square4<<"|" <<Square5<<"|" <<Square6<<endl;
cout<<"-+-+-" <<endl;
cout<<Square7<<"|" <<Square8<<"|" <<Square9<<endl;
char Playermark;
if (Playerturn == 1){
Playermark = 'X' ;
} else {
Playermark = 'O' ;
}
cout<<"It is currently player " <<Playerturn<<"'s turn." <<endl;
bool Validmove;
do {
Validmove = true ;
char input;
cin>>input;
if (input == '1' && Square1 == '1' ){
Square1 = Playermark;
} else if (input == '2' && Square2 == '2' ){
Square2 = Playermark;
} else if (input == '3' && Square3 == '3' ){
Square3 = Playermark;
} else if (input == '4' && Square4 == '4' ){
Square4 = Playermark;
} else if (input == '5' && Square5 == '5' ){
Square5 = Playermark;
} else if (input == '6' && Square6 == '6' ){
Square6 = Playermark;
} else if (input == '7' && Square7 == '7' ){
Square7 = Playermark;
} else if (input == '8' && Square8 == '8' ){
Square8 = Playermark;
} else if (input == '9' && Square9 == '9' ){
Square9 = Playermark;
} else {
cout<<"Invalid Entry" <<endl;
Validmove = false ;
}
} while (!Validmove);
Gameover = false ;
bool Wingame = true ;
if (Square1 != '1' ){
if (Square1 == Square2 && Square1 == Square3 && Square2 == Square3){
Gameover = true ;
Wingame = true ;
} else if (Square1 == Square4 && Square1 == Square7 && Square4 == Square7){
Gameover = true ;
Wingame = true ;
}
} if (Square9 != '9' ){
if (Square9 == Square3 && Square6 == Square9 && Square3 == Square6){
Gameover = true ;
Wingame = true ;
} if (Square9 == Square8 && Square9 == Square7 && Square8 == Square7){
Gameover = true ;
Wingame = true ;
}
} if (Square5 != '5' ){
if (Square5 == Square1 && Square5 == Square9 && Square9 == Square1){
Gameover = true ;
Wingame = true ;
} else if (Square5 == Square3 && Square5 == Square7 && Square7 == Square3){
Gameover = true ;
Wingame = true ;
} else if (Square5 == Square4 && Square5 == Square6 && Square6 == Square4){
Gameover = true ;
Wingame = true ;
} else if (Square5 == Square2 && Square5 == Square8 && Square8 == Square2){
Gameover = true ;
Wingame = true ;
}
}
if (Square1 != '1' && Square2 != '2' && Square3 != '3' && Square4 != '4' && Square5 != '5'
&& Square6 != '6' && Square7 != '7' && Square8 != '8' && Square9 != '9' && !Gameover){
Gameover = true ;
Wingame = false ;
}
if (Gameover){
if (Wingame){
cout<<"Player: " <<Playerturn<<" has won!" <<endl;
}
cout<<Square1<<"|" <<Square2<<"|" <<Square3<<endl;
cout<<"-+-+-" <<endl;
cout<<Square4<<"|" <<Square5<<"|" <<Square6<<endl;
cout<<"-+-+-" <<endl;
cout<<Square7<<"|" <<Square8<<"|" <<Square9<<endl;
char yn;
cout<<"Play again y/n?" <<endl;
cin>>yn;
if (yn = 'y' ){
Gameover = false ;
Square1 = '1' ;
Square2 = '2' ;
Square3 = '3' ;
Square4 = '4' ;
Square5 = '5' ;
Square6 = '6' ;
Square7 = '7' ;
Square8 = '8' ;
Square9 = '9' ;
}
Playerturn = 1;
} else {
if (Playerturn == 1){
Playerturn = 2;
} else {
Playerturn = 1;
}
}
} while (!Gameover);
}
Apr 22, 2013 at 5:32am UTC
Last edited on Apr 23, 2013 at 2:18am UTC
Topic archived. No new replies allowed.