Old 18-10-2008   #1 (permalink)
Monster Member
 
Join Date: Sep 2008
Location: Melbourne
Age: 17
Posts: 2,406
Rep Power: 3
Default Tic Tac Toe - C++

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;
}
Wabtonio is offline   Reply With Quote
Old 18-10-2008   #2 (permalink)
Pro Member
 
empske's Avatar
 
Join Date: Jan 2007
Location: ZULU
Age: 17
Posts: 6,719
Rep Power: 0
Default

Wow

That's awesome.

Gw man
__________________

odin : atleast i didnt join gotgames in 2009

C:\Documents and Settings\Admin\Desktop\DdudFACE.png
empske is offline   Reply With Quote
Old 18-10-2008   #3 (permalink)
Honorary Member
 
cynick's Avatar
 
Join Date: Jun 2008
Location: Melbourne
Age: 16
Posts: 1,516
Rep Power: 2
Default

yea wd bro
__________________

#teamdeviate - www.teamdeviate.com.au - css.online.aus
vitaL :: Schecken :: dimsuM :: albstR :: cyniCk :: Patriot :: heretic
cynick is offline   Reply With Quote
Old 18-10-2008   #4 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

Very nice work man. Nice neat coding. Try in the future though not to use the goto function. It can be avoided.
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 18-10-2008   #5 (permalink)
Honorary Member
 
joshkent's Avatar
 
Join Date: Jun 2008
Location: Brisbane
Age: 14
Posts: 1,458
Rep Power: 2
Default

Nice and clean, as sarge said, it CAN be avoided
__________________
SUNDER
joshkent~
joshkent is offline   Reply With Quote
Old 18-10-2008   #6 (permalink)
Pro Member
 
kotso's Avatar
 
Join Date: Sep 2007
Location: FORUM POLICE
Age: 17
Posts: 5,050
Rep Power: 7
Send a message via MSN to kotso
Default

AHAH THAT IS AWESOME!
__________________
................._@@@__
......_____//____?__\________
- ---o--------FORUM-POLICE---@)
-----` --(@)======+====(@)--
kotso is offline   Reply With Quote
Old 18-10-2008   #7 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

Also i cant run the EXE, i got a configuration error.
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 18-10-2008   #8 (permalink)
God
Pro Member
 
God's Avatar
 
Join Date: Jul 2006
Location: Adelaide
Posts: 5,311
Rep Power: 8
Default

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
__________________
God is offline   Reply With Quote
Old 18-10-2008   #9 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote
Old 19-10-2008   #10 (permalink)
Monster Member
 
SargeRX8's Avatar
 
Join Date: Nov 2006
Location: Merrylands 2160
Age: 21
Posts: 3,687
Rep Power: 6
Default

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.
__________________

Click the sig to watch my Rambo Video

HuggyCHEA[2f][2sxc]: im a gentleman gee
HuggyCHEA[2f][2sxc]: but get me drunk broo, ill grab your tits
SargeRX8 is offline   Reply With Quote
Old 19-10-2008   #11 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote
Old 27-10-2008   #12 (permalink)
ivo
GotGames Moderator
 
ivo's Avatar
 
Join Date: Jun 2006
Location: Vic
Posts: 2,060
Rep Power: 5
Default

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
ivo is offline   Reply With Quote
Old 27-10-2008   #13 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Do a Min-Max algorithm for it.
__________________
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.
Khaless is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +10. The time now is 05:32 AM.


Powered by vBulletin® Version 3.7.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0