Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

steve010

Never Made a Game

This topic is 5722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you''ve seen my last post you know I''m trying to make Zelda, but a friend urged me to make a text based game first, so I''m gunna code tic tac toe and get it over with. I''ll post the code up soon...

Share this post


Link to post
Share on other sites
Advertisement
Tic Tac toe isn''t that hard of a game. You should try a real Text RPG type game. It will most likely help you out in the long run and will give you valuable experience on the components of an RPG.

Share this post


Link to post
Share on other sites
It depends on how much you know of the programming language you use. I am very new to C++ myself and truth be told to programming too. I tried to make a text based TIC-TAC-TOE and rather soon figured out i knew WAY to little of C++. But i suppose that TIC-TAC-TOE is a good thing to start with...

Share this post


Link to post
Share on other sites
I wrote tic-tac-toe. No AI thought, it scares me. Kept me from finishing my pacman game . I''m gonna pick it back up one day
and try again.



AtomicGames[http://www.geocities.com/agdev2k/]

Share this post


Link to post
Share on other sites
What on earth is tic-tac-toe? I heard about that program when I was about five (Amiga + Amstrad 5inch demos!) and now it''s coming back to haunt me!

Stuffing feathers up your ass does not make you a chicken.

Share this post


Link to post
Share on other sites
Ive pasted my TicTacToe code for ya here. Im not sayin just copy it but if you are going to make it and need understanding at any point lookin at it might help. Its got some real primative AI too.

tictactoe.h

  
using namespace std;

extern int choice;

//Function declarations


void drawboard (); //draws out the tictactoe board

void pchoice (); //gets the players choice

void compchoice (); //gets the computers choice

void wincheck (); //checks if a player has won

int AI (int);//finds which sqare the comp chooses



funcdefs.cpp

  
//Defining functions and globals

#include <iostream>
#include <time.h>
#include "TicTacToe.h"
using namespace std;

//Globals


int player = 2; //which player is choosing

int choice = 0; //which sqare they chose

int randnum = 0; //for finding rands

int max = 0; //max for rands


char xoboard [9] = {''1'', ''2'', ''3'',
''4'', ''5'', ''6'', //contents of the board

''7'', ''8'', ''9''};

int sqaremark [9] = { 0, 0, 0,
0, 0, 0, //puts marker on chosen sqares

0, 0, 0, }; //for wincheck






/*********************DRAWBOARD********************/

void drawboard () //makes drawing of board on the screen
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; //makeshift clearscreen
cout << " " << xoboard [0] << " | " << xoboard [1] << " | " << xoboard [2] << " " << endl;
cout << "---+---+---" << endl;
cout << " " << xoboard [3] << " | " << xoboard [4] << " | " << xoboard [5] << " " << endl;
cout << "---+---+---" << endl;
cout << " " << xoboard [6] << " | " << xoboard [7] << " | " << xoboard [8] << " " << endl;
cout << "\n\n\n\n\n\n\n\n\n\n\n";
return;
}



/*********************PCHOICE********************/

void pchoice () //finds which sqare is chosen and switches the player
{
psqarechoice:
//for player
cout << "Pick a sqare: ";
cin >> choice;

if (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0))//colision detection
{
cout << endl << "That sqare is not accesable" << endl;
goto psqarechoice; //return back to sqare choosing
}

else //if sqare is not allready chosen
{
sqaremark [choice - 1] = 1; //marks a 1 on sqaremark
xoboard [choice - 1] = ''O''; //drawes ''O'' in chosen srare and makes player = 2
player = 2;
}
return;
}



/*********************COMPCHOICE********************/

void compchoice ()
{
compsqarechoice:
AI (choice);

if (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0))//colision detection
{
goto compsqarechoice; //return back to sqare choosing
}

else
{
sqaremark [choice - 1] = 2; //marks a 2 on sqaremark
xoboard [choice - 1] = ''X''; //drawes ''X'' in chosen srare and makes player = 1
player = 1;
}
return;
}


/*********************WINCHECK********************/

void wincheck ()
{
if ( //rows
(sqaremark [0] == 1 && sqaremark [1] == 1 && sqaremark [2] == 1) ||
(sqaremark [3] == 1 && sqaremark [4] == 1 && sqaremark [5] == 1) ||
(sqaremark [6] == 1 && sqaremark [7] == 1 && sqaremark [8] == 1) ||
//coloms
(sqaremark [0] == 1 && sqaremark [3] == 1 && sqaremark [6] == 1) ||
(sqaremark [1] == 1 && sqaremark [4] == 1 && sqaremark [7] == 1) ||
(sqaremark [2] == 1 && sqaremark [5] == 1 && sqaremark [8] == 1) ||
//diagonals
(sqaremark [0] == 1 && sqaremark [4] == 1 && sqaremark [8] == 1) ||
(sqaremark [6] == 1 && sqaremark [4] == 1 && sqaremark [2] == 1)
)

{
cout << "Player is the winner" << endl << endl; //displays if player 1 has 3 in a row
}


else if ( //rows
(sqaremark [0] == 2 && sqaremark [1] == 2 && sqaremark [2] == 2) ||
(sqaremark [3] == 2 && sqaremark [4] == 2 && sqaremark [5] == 2) ||
(sqaremark [6] == 2 && sqaremark [7] == 2 && sqaremark [8] == 2) ||
//coloms
(sqaremark [0] == 2 && sqaremark [3] == 2 && sqaremark [6] == 2) ||
(sqaremark [1] == 2 && sqaremark [4] == 2 && sqaremark [7] == 2) ||
(sqaremark [2] == 2 && sqaremark [5] == 2 && sqaremark [8] == 2) ||
//diagonals
(sqaremark [0] == 2 && sqaremark [4] == 2 && sqaremark [8] == 2) ||
(sqaremark [6] == 2 && sqaremark [4] == 2 && sqaremark [2] == 2)
)
{
cout << "Computer is the winner" << endl << endl; //displayes if player 2 has 3 in a row
}
return;
}



/*********************AI********************/


int AI (int) //finds the sqare the computer chooses

{
//For the win

//across

//line 1

if ( sqaremark [2] == 1 && sqaremark [1] == 1 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [2] == 1 && sqaremark [0] == 1 && sqaremark [1] == 0)
{ choice = 2; return choice; }
if ( sqaremark [1] == 1 && sqaremark [0] == 1 && sqaremark [2] == 0)
{ choice = 3; return choice; }
//line 2

if ( sqaremark [5] == 1 && sqaremark [4] == 1 && sqaremark [3] == 0)
{ choice = 1; return choice; }
if ( sqaremark [5] == 1 && sqaremark [3] == 1 && sqaremark [4] == 0)
{ choice = 2; return choice; }
if ( sqaremark [4] == 1 && sqaremark [3] == 1 && sqaremark [5] == 0)
{ choice = 3; return choice; }
//line 3

if ( sqaremark [8] == 1 && sqaremark [7] == 1 && sqaremark [6] == 0)
{ choice = 1; return choice; }
if ( sqaremark [8] == 1 && sqaremark [6] == 1 && sqaremark [7] == 0)
{ choice = 2; return choice; }
if ( sqaremark [7] == 1 && sqaremark [6] == 1 && sqaremark [8] == 0)
{ choice = 3; return choice; }
//down

//line 1

if ( sqaremark [6] == 1 && sqaremark [3] == 1 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [6] == 1 && sqaremark [0] == 1 && sqaremark [3] == 0)
{ choice = 2; return choice; }
if ( sqaremark [3] == 1 && sqaremark [0] == 1 && sqaremark [6] == 0)
{ choice = 3; return choice; }
//line 2

if ( sqaremark [7] == 1 && sqaremark [4] == 1 && sqaremark [1] == 0)
{ choice = 1; return choice; }
if ( sqaremark [7] == 1 && sqaremark [1] == 1 && sqaremark [4] == 0)
{ choice = 2; return choice; }
if ( sqaremark [4] == 1 && sqaremark [1] == 1 && sqaremark [7] == 0)
{ choice = 3; return choice; }
//line 3

if ( sqaremark [8] == 1 && sqaremark [5] == 1 && sqaremark [2] == 0)
{ choice = 1; return choice; }
if ( sqaremark [8] == 1 && sqaremark [2] == 1 && sqaremark [5] == 0)
{ choice = 2; return choice; }
if ( sqaremark [5] == 1 && sqaremark [2] == 1 && sqaremark [8] == 0)
{ choice = 3; return choice; }
//diagonal

//diag 1

if ( sqaremark [8] == 1 && sqaremark [4] == 1 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [8] == 1 && sqaremark [0] == 1 && sqaremark [4] == 0)
{ choice = 2; return choice; }
if ( sqaremark [4] == 1 && sqaremark [0] == 1 && sqaremark [8] == 0)
{ choice = 3; return choice; }
//diag 2

if ( sqaremark [6] == 1 && sqaremark [4] == 1 && sqaremark [2] == 0)
{ choice = 1; return choice; }
if ( sqaremark [6] == 1 && sqaremark [2] == 1 && sqaremark [4] == 0)
{ choice = 2; return choice; }
if ( sqaremark [4] == 1 && sqaremark [2] == 1 && sqaremark [6] == 0)
{ choice = 3; return choice; }

//For the block

//across

//line 1

if ( sqaremark [2] == 2 && sqaremark [1] == 2 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [2] == 2 && sqaremark [0] == 2 && sqaremark [1] == 0)
{ choice = 2; return choice; }
if ( sqaremark [1] == 2 && sqaremark [0] == 2 && sqaremark [2] == 0)
{ choice = 3; return choice; }
//line 2

if ( sqaremark [5] == 2 && sqaremark [4] == 2 && sqaremark [3] == 0)
{ choice = 4; return choice; }
if ( sqaremark [5] == 2 && sqaremark [3] == 2 && sqaremark [4] == 0)
{ choice = 5; return choice; }
if ( sqaremark [4] == 2 && sqaremark [3] == 2 && sqaremark [5] == 0)
{ choice = 6; return choice; }
//line 3

if ( sqaremark [8] == 2 && sqaremark [7] == 2 && sqaremark [6] == 0)
{ choice = 7; return choice; }
if ( sqaremark [8] == 2 && sqaremark [6] == 2 && sqaremark [7] == 0)
{ choice = 8; return choice; }
if ( sqaremark [7] == 2 && sqaremark [6] == 2 && sqaremark [8] == 0)
{ choice = 9; return choice; }
//down

//line 1

if ( sqaremark [6] == 2 && sqaremark [3] == 2 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [6] == 2 && sqaremark [0] == 2 && sqaremark [3] == 0)
{ choice = 4; return choice; }
if ( sqaremark [3] == 2 && sqaremark [0] == 2 && sqaremark [6] == 0)
{ choice = 7; return choice; }
//line 2

if ( sqaremark [7] == 2 && sqaremark [4] == 2 && sqaremark [1] == 0)
{ choice = 2; return choice; }
if ( sqaremark [7] == 2 && sqaremark [1] == 2 && sqaremark [4] == 0)
{ choice = 5; return choice; }
if ( sqaremark [4] == 2 && sqaremark [1] == 2 && sqaremark [7] == 0)
{ choice = 8; return choice; }
//line 3

if ( sqaremark [8] == 2 && sqaremark [5] == 2 && sqaremark [2] == 0)
{ choice = 3; return choice; }
if ( sqaremark [8] == 2 && sqaremark [2] == 2 && sqaremark [5] == 0)
{ choice = 6; return choice; }
if ( sqaremark [5] == 2 && sqaremark [2] == 2 && sqaremark [8] == 0)
{ choice = 9; return choice; }
//diagonal

//diag 1

if ( sqaremark [8] == 2 && sqaremark [4] == 2 && sqaremark [0] == 0)
{ choice = 1; return choice; }
if ( sqaremark [8] == 2 && sqaremark [0] == 2 && sqaremark [4] == 0)
{ choice = 5; return choice; }
if ( sqaremark [4] == 2 && sqaremark [0] == 2 && sqaremark [8] == 0)
{ choice = 9; return choice; }
//diag 2

if ( sqaremark [6] == 2 && sqaremark [4] == 2 && sqaremark [2] == 0)
{ choice = 3; return choice; }
if ( sqaremark [6] == 2 && sqaremark [2] == 2 && sqaremark [4] == 0)
{ choice = 5; return choice; }
if ( sqaremark [4] == 2 && sqaremark [2] == 2 && sqaremark [6] == 0)
{ choice = 7; return choice; }

//middle sqare

if ( sqaremark [4] == 0 )
{ choice = 5; return choice; }

//new rand seed

srand((unsigned)time( NULL ));

//Corners

if ( sqaremark [0] == 0 || sqaremark [2] == 0 || sqaremark [6] == 0 || sqaremark [8] == 0 )
{
int max = 4, randnum = rand() % max; //get rand between 0 and 4

if ( randnum == 0)
{ choice = 1; return choice; }
if ( randnum == 1)
{ choice = 3; return choice; }
if ( randnum == 2)
{ choice = 7; return choice; }
if ( randnum == 3)
{ choice = 9; return choice; }
}

//Anything else

else
{
int max = 9; randnum = rand() % max;
choice = randnum + 1; return choice;
}
}


tictactoe.cpp

  
//program loop

#include <iostream>
#include <time.h>
#include "TicTacToe.h"
using namespace std;

extern int choice; //sqare player chooses

extern int player; //what player it is (1 or 2)

extern int randnum; //for finding rands

extern int max; //max for rands

extern char xoboard []; //the 9x9 board contents

extern int sqaremark [];//marked sqares


int main ()
{
for (int i = 9; i > 0; i--) //execute loop 9 times

{
drawboard (); //draws board

wincheck (); //checks for a winner

if (player == 1)
{ pchoice (); } //gets player choice

else
{ compchoice (); }//gets comps choice

}

return 0;
}

Share this post


Link to post
Share on other sites
All of those if statements hurt my brain. There''s gotta be a fairy simple algorithim to determine the best square to choose, assuming the goal is a tie (as it should be in tic-tac-toe).
/me suddenly knows what he''s going to spend the night doing.

Share this post


Link to post
Share on other sites
Yeah i think sombody suggested the minmax algorith or sumthin like that but i was more in the mood for some cuttin and pastin, well it might of been that or that i have no idea what a minmax algorith is, or just a plain algorithm for that matter. Maybe i should research that.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!