Post: Tic Tac Toe [C++]
11-03-2012, 06:01 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I got bored and decided I should improve my skills in C++. I was thinking of something easy to make, yet something to challenge my skills a little bit. I decided to make tic tac toe! It's pretty basic but handles all the fundamentals. Let me know what you think!

Download: You must login or register to view this content.

Download Source (cpp): You must login or register to view this content.


I left the source but I don't want any skids using it stare you can learn from it but don't steal it plz.
11-03-2012, 06:35 PM #2
Sloth
Banned
Originally posted by GAMER View Post
I got bored and decided I should improve my skills in C++. I was thinking of something easy to make, yet something to challenge my skills a little bit. I decided to make tic tac toe! It's pretty basic but handles all the fundamentals. Let me know what you think!

Download: You must login or register to view this content.

Download Source (cpp): You must login or register to view this content.


I left the source but I don't want any skids using it stare you can learn from it but don't steal it plz.

I just downloaded it and had a quick game. It's pretty cool man. You've got some good C++ skills. you should try building a bigger project instead of mini games.

your a great dev Happy
11-03-2012, 07:11 PM #3
Originally posted by KronosCloud View Post
I just downloaded it and had a quick game. It's pretty cool man. You've got some good C++ skills. you should try building a bigger project instead of mini games.

your a great dev Happy


Thanks man, I'm glad to get good feedback. I've been mainly programming in C# and PHP for the past year or so and I've been trying to step it up and learn new languages. Do you have any ideas of another project to make? Happy
11-03-2012, 07:53 PM #4
Sloth
Banned
Originally posted by GAMER View Post
Thanks man, I'm glad to get good feedback. I've been mainly programming in C# and PHP for the past year or so and I've been trying to step it up and learn new languages. Do you have any ideas of another project to make? Happy


You could make maybe a File Encrypter or a Program Authentication System. although they are very hard.
11-03-2012, 07:56 PM #5
Originally posted by KronosCloud View Post
You could make maybe a File Encrypter or a Program Authentication System. although they are very hard.


I have actually already done both. :p They weren't very hard at all. I'm just not sure what to do haha.
11-03-2012, 08:12 PM #6
Vanz
Z32 Love <3
Originally posted by GAMER View Post
I got bored and decided I should improve my skills in C++. I was thinking of something easy to make, yet something to challenge my skills a little bit. I decided to make tic tac toe! It's pretty basic but handles all the fundamentals. Let me know what you think!

Download: You must login or register to view this content.

Download Source (cpp): You must login or register to view this content.


I left the source but I don't want any skids using it stare you can learn from it but don't steal it plz.


I keep getting a error You must login or register to view this content.
11-03-2012, 08:27 PM #7
Sloth
Banned
Originally posted by .Vanz View Post
I keep getting a error You must login or register to view this content.

Websites backup go to the url again.
11-03-2012, 08:29 PM #8
Vanz
Z32 Love <3
Originally posted by KronosCloud View Post
Websites backup go to the url again.

I still get a error. :fa:
11-03-2012, 08:35 PM #9
Originally posted by .Vanz View Post
I still get a error. :fa:


You're getting an error because of the host. It's run off of a free host so your firewall probably tries and blocks it.

Edit:
If you want the source:

    
//Tic Tac Toe App
//premier-gamer

//header include
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"

//using namespace
using namespace System;

//initial grid
int grid[3][3];

int pickPlayer()
{
return rand() % 2 + 1;
}

void resetScore()
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
grid[i][j] = 0;
}
}
}

void drawGrid()
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
String^ ch;
switch (grid[i][j])
{
case 0 : ch = "-"; break;
case 1 : ch = "X"; break;
case 2 : ch = "O";
}
Console::Write( "\t" + ch );
}
Console::Write("\n\n");
}
}

array<int>^ countOwns(int player)
{
int size = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if(grid[i][j] == player)
size++;
int index = 0;
array<int>^ owns = gcnew array<int>(size);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if(grid[i][j] == player)
{
owns[index] = ((i*3) + j)+1;
index++;
}
}
}
return owns;
}

int magicSquare( int x )
{
switch(x)
{
case 1: return 8;
case 2: return 1;
case 3: return 6;
case 4: return 3;
case 5: return 5;
case 6: return 7;
case 7: return 4;
case 8: return 9;
case 9: return 2;
}
}

bool checkWinner(int player)
{
bool win = false;
array<int>^ owns = countOwns(player);
for(int x=0;x<owns->Length;x++)
{
for(int y=(x+1);y<owns->Length;y++)
{
for(int z=(y+1);z<owns->Length;z++)
{
//this algorithm sucks donkey balls...
if( (magicSquare(owns[x]) + magicSquare(owns[y]) + magicSquare(owns[z]) == 15) && owns->Length > 2)
win = true;
}
}
}
return win;
}

void showHelpScreen()
{
Console::Clear();
Console::Write("\t================================\n\t= Tic Tac Toe =\n\t= Made By: PREMIER-GAMER =\n\t= =\n\t= -= INSTRUCTIONS =- =\n\t= 1. Enter a number (1-9) =\n\t= 2. Switch Characters =\n\t= 3. Try and get 3 in a row =\n\t================================");
Console::WriteLine("\n\n\tPress any key to return.");
Console::ReadKey();
Console::Clear();
}

void showMainMenu()
{
Console::WriteLine("\n\n\t\tX\tO\tX\n\n\t\tO\tX\t-\n\n\t\tO\t-\tX\n\n\n\t\t1. Start Game\n\t\t2. Help\n\t\t3. Exit");
char selection = Console::ReadKey().KeyChar;
switch(selection)
{
case '1': break;
case '2': showHelpScreen();Console::Clear();showMainMenu(); break;
case '3': Environment::Exit(0); break;
default: Console::Clear(); showMainMenu();
}
Console::Clear();
}

int main()
{
Console::Title = "Tic Tac Toe";
srand ( time(NULL) );
bool gameStart = true;
bool firstRun = true;
int number,row,ovr;
int player = pickPlayer();
resetScore();
while ( gameStart )
{
if(firstRun)
{
firstRun = false;
showMainMenu();
}
drawGrid();
Console::WriteLine("\n\nCurrent Player: " + player + "\nSelect a spot to choose. (1-9)\n\nType \"help\" for more information.\n\n");
String^ input = Console::ReadLine(); //get this incase of help.
if(input == "help")
{
showHelpScreen();
drawGrid();
}
else
{
try{number = Convert::ToInt32(input);}
catch(Exception^ ex){number = 12;}
}
if(number > 0 && number < 10)
{
row = System::Math::Floor(number/3);
ovr = number-(3*row)-1;
}
else
{
Console::Clear();
continue;
}

if(grid[row][ovr] == 0)
{
grid[row][ovr] = player;

if(checkWinner(player))
{
Console::Clear();
Console::WriteLine(L"Player " + player + " is the winner!\n\nWould you like to play again? (y/n)");
char input = Console::ReadKey().KeyChar;
if(input == 'y'Winky Winky
{
Console::Clear();
resetScore();
player = pickPlayer();
continue;
}
else
{
Console::Clear();
showMainMenu();
}
}
else
{
if(countOwns(player)->Length >= 5)
{
Console::Clear();
Console::WriteLine(L"Looks like it's a tie!\n\nWould you like to play again? (y/n)");
char input = Console::ReadKey().KeyChar;
if(input == 'y'Winky Winky
{
Console::Clear();
resetScore();
player = pickPlayer();
continue;
}
else
{
Console::Clear();
showMainMenu();
}
}
}

if(player == 1)
player++;
else
player--;
}
else
{
Console::Clear();
continue;
}
Console::Clear();
}
return 0;
}

11-06-2012, 08:45 AM #10
Complete Speed
Do a barrel roll!
Originally posted by GAMER View Post
I got bored and decided I should improve my skills in C++. I was thinking of something easy to make, yet something to challenge my skills a little bit. I decided to make tic tac toe! It's pretty basic but handles all the fundamentals. Let me know what you think!

Download: You must login or register to view this content.

Download Source (cpp): You must login or register to view this content.


I left the source but I don't want any skids using it stare you can learn from it but don't steal it plz.


great! but should it really be that long of code for tic tac toe? you didn't use some of the most vital parts of c++ to get more optimized code. such as classes and templates exceptions etc. but other than that it's pretty nice. want another game to make? try from scratch make a game which allows you to enter your players name and give them a health bar and give them a list of items to choose from when they choose it depending on what it is it could do negative damage or positive influence etc. then you can make a text rpg. that'd be sick. you got good potential. just need some more optimization.

tl;dr
codes to long(good job though)
use classes templates and other of c++ to make code smaller.
make a rpg with listed items and health
Last edited by Complete Speed ; 11-06-2012 at 08:47 AM.

The following user thanked Complete Speed for this useful post:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo