|
|
#1 (permalink) |
|
Monster Member
Join Date: Sep 2008
Location: Melbourne
Age: 17
Posts: 2,406
Rep Power: 3
|
This is a console Tic Tac Toe game made with C++.
It's a two player game, and you type in the number for the space you want to put your letter in. ScreenShot: ![]() If you don't wanna compile it, here's the exe: http://www.wabtonio.net/stuff/TicTacToe.exe Enjoy ![]() Code:
//Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
bool gamedone,turn;
int choice,player;
string space[10] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
void win(bool x)
{
if (x == true) {
player = (player == 1) ? 2 : 1;
cout << "Player " << player << " Won!!!" << endl << endl;
}
else cout << "It's a stale mate!" << endl << endl;
gamedone = true;
system("pause");
}
void checkwin()
{
if (space[1] == space[2] && space[2] == space[3]) win(true);
else if (space[4] == space[5] && space[5] == space[6]) win(true);
else if (space[7] == space[8] && space[8] == space[9]) win(true);
else if (space[1] == space[4] && space[4] == space[7]) win(true);
else if (space[2] == space[5] && space[5] == space[8]) win(true);
else if (space[3] == space[6] && space[6] == space[9]) win(true);
else if (space[1] == space[5] && space[5] == space[9]) win(true);
else if (space[3] == space[5] && space[5] == space[7]) win(true);
else if (space[1] != "1" && space[2] != "2" && space[3] != "3" && space[4] != "4" && space[5] != "5" && space[6] != "6" && space[7] != "7" && space[8] != "8" && space[9] != "9") win(false);
}
int main()
{
start:
gamedone = false;
player = 1;
guess:
system("cls");
cout << "Tic Tac Toe" << endl << endl;
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << space[1] << " | " << space[2] << " | " << space[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << space[4] << " | " << space[5] << " | " << space[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << space[7] << " | " << space[8] << " | " << space[9] << endl;
cout << " | | " << endl << endl;
checkwin();
if (gamedone == true) goto start;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
if (choice == 1 && space[1] == "1") {
space[1] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 2 && space[2] == "2") {
space[2] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 3 && space[3] == "3") {
space[3] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 4 && space[4] == "4") {
space[4] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 5 && space[5] == "5") {
space[5] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 6 && space[6] == "6") {
space[6] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 7 && space[7] == "7") {
space[7] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 8 && space[8] == "8") {
space[8] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
if (choice == 9 && space[9] == "9") {
space[9] = (player == 1) ? "X" : "O";
player = (player == 1) ? 2 : 1;
}
goto guess;
}
|
|
|
|
|
|
|
|
#3 (permalink) |
|
Honorary Member
Join Date: Jun 2008
Location: Melbourne
Age: 16
Posts: 1,516
Rep Power: 2
|
yea wd bro
__________________
#teamdeviate - www.teamdeviate.com.au - css.online.aus vitaL :: Schecken :: dimsuM :: albstR :: cyniCk :: Patriot :: heretic |
|
|
|
|
|
#5 (permalink) |
|
Honorary Member
Join Date: Jun 2008
Location: Brisbane
Age: 14
Posts: 1,458
gXboxLive Leaderboard: 95th
Rep Power: 2
|
Nice and clean, as sarge said, it CAN be avoided
__________________
|
|
|
|
|
|
#8 (permalink) |
|
Pro Member
Join Date: Jul 2006
Location: Adelaide
Posts: 5,311
Rep Power: 8
|
Wish C had Boolean types, probs a cleaner way to do the else ifs but i cbf thinking of one (i'd do the exact same thing in my code).
Edit: cept personally id keep main() smaller with some unneeded aesthetic functions
__________________
|
|
|
|
|
|
#9 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
C++ has bool...
C has your very own: typedef enum {false = 0, true = 1} bool;
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#10 (permalink) |
|
Monster Member
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
|
Would have been simpler to do a win function if he had used a 2D array? I remember doing something similar to this(it was a game where it scrambles the numbers on a grid and then you gotta sort em out and another game where you load an image like a puzzle and you gotta unjumble it) and used a 2D array and my check win function was i think alot shorter.
|
|
|
|
|
|
#11 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Yeah, a 2D array might make it easier as you can break it into 3 cases, check rows, check cols and check diagonals.
But still a 1D approach really allows you to confuse people... def check; [[1,1,1,0,0,0,0,0,0],[0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,1,1,1],[1,0,0,1,0,0,1,0,0],[0,1,0,0,1,0,0,1,0],[0,0,1,0,0,1,0,0,1],[1,0,0,0,1,0,0,0,1],[0,0,1,0,1,0,1,0,0]].each { |m| s = SyncEnumerator.new(@b, m); w = 0; s.each { |r| w += r[0] * r[1] }; if w.abs == 3; return w/3; end }; @b.each {|i| return false if i == 0}; return 0; end ** disclaimer: untested ... But yeah, perhaps try to avoid the goto's as Sarge said.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#12 (permalink) |
|
GotGames Moderator
Join Date: Jun 2006
Location: Vic
Posts: 2,060
Rep Power: 5
|
NEXT:
A) Let user choose whether to play 1st or not B) Have two difficulties - one you can win against, one you can only draw or lose against (tictactoe is a dead draw with best play from both sides) GOGOGO
__________________
"If you wanna get it through their head, talk to them ragely" - fakeh |
|
|
|







