Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

tic tac toe

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

tic tac toe

Postby ShadowTH on Sat Oct 03, 2009 12:16 am

I decided to try my best at a tic tac toe game. What do you think Anti RTFM? ( soz i don't know your name yet )
Also, there's no computer player. It just shows the spaces and if you get 3 in a row you win lol. But is the code good
for what its doing? Thanks!

Code: Select all
#include <iostream>
using namespace std;
bool breakloop = false;
int exitg = 1;
char a1 = 'I';
char a2 = 'I';
char a3 = 'I';
char b1 = 'I';
char b2 = 'I';
char b3 = 'I';
char c1 = 'I';
char c2 = 'I';
char c3 = 'I';
char x = 8;
int y = 0;

int graphics()
{
     cout << "a  "; cout << a1; cout << a2; cout << a3 << endl;
     cout << "b  "; cout << b1; cout << b2; cout << b3 << endl;
     cout << "c  "; cout << c1; cout << c2; cout << c3 << endl;
     cout << " \n"; //spacer
     cout << "   1"; cout << "2"; cout << "3" << endl;
     cout << "\n";
}

void game()
{
     if (x == 'a' && y == 1) { a1 = 'x'; }
     if (x == 'a' && y == 2) { a2 = 'x'; }
     if (x == 'a' && y == 3) { a3 = 'x'; }
     if (x == 'b' && y == 1) { b1 = 'x'; }
     if (x == 'b' && y == 2) { b2 = 'x'; }
     if (x == 'b' && y == 3) { b3 = 'x'; }
     if (x == 'c' && y == 1) { c1 = 'x'; }
     if (x == 'c' && y == 2) { c2 = 'x'; }
     if (x == 'c' && y == 3) { c3 = 'x'; }
}     

void test()
{
     if (a1 == 'x' && a2 == 'x' && a3 == 'x') { breakloop = true; }
     if (b1 == 'x' && b2 == 'x' && b3 == 'x') { breakloop = true; }
     if (c1 == 'x' && c2 == 'x' && c3 == 'x') { breakloop = true; }
     if (a1 == 'x' && b2 == 'x' && c3 == 'x') { breakloop = true; }
     if (a1 == 'x' && b1 == 'x' && c1 == 'x') { breakloop = true; }
     if (a2 == 'x' && b2 == 'x' && c2 == 'x') { breakloop = true; }
     if (a3 == 'x' && b3 == 'x' && c3 == 'x') { breakloop = true; }
}

int main()
{
    while (exitg < 2)
    {
    graphics();
    cout << "Your turn. Enter X coordinate (letter): ";
    cin >> x;
    system("cls");
    cout << "Enter Y coordinate (number): ";
    cin >> y;
    system("cls");
    game();
    //graphics();
    test();
    if (breakloop == true)
       {
       break;
       }
    }
   cout << "You won!";
   
   int thing = 5;
   cin >> thing;
}










ShadowTH
 
Posts: 6
Joined: Tue Aug 18, 2009 7:39 am

Re: tic tac toe

Postby Ruddie on Sun Oct 04, 2009 6:14 am

ShadowTH wrote:I decided to try my best at a tic tac toe game. What do you think Anti RTFM? ( soz i don't know your name yet )
Also, there's no computer player. It just shows the spaces and if you get 3 in a row you win lol. But is the code good
for what its doing? Thanks!

Code: Select all
#include <iostream>
using namespace std;
bool breakloop = false;
int exitg = 1;
char a1 = 'I';
char a2 = 'I';
char a3 = 'I';
char b1 = 'I';
char b2 = 'I';
char b3 = 'I';
char c1 = 'I';
char c2 = 'I';
char c3 = 'I';
char x = 8;
int y = 0;

int graphics()
{
     cout << "a  "; cout << a1; cout << a2; cout << a3 << endl;
     cout << "b  "; cout << b1; cout << b2; cout << b3 << endl;
     cout << "c  "; cout << c1; cout << c2; cout << c3 << endl;
     cout << " \n"; //spacer
     cout << "   1"; cout << "2"; cout << "3" << endl;
     cout << "\n";
}

void game()
{
     if (x == 'a' && y == 1) { a1 = 'x'; }
     if (x == 'a' && y == 2) { a2 = 'x'; }
     if (x == 'a' && y == 3) { a3 = 'x'; }
     if (x == 'b' && y == 1) { b1 = 'x'; }
     if (x == 'b' && y == 2) { b2 = 'x'; }
     if (x == 'b' && y == 3) { b3 = 'x'; }
     if (x == 'c' && y == 1) { c1 = 'x'; }
     if (x == 'c' && y == 2) { c2 = 'x'; }
     if (x == 'c' && y == 3) { c3 = 'x'; }
}     

void test()
{
     if (a1 == 'x' && a2 == 'x' && a3 == 'x') { breakloop = true; }
     if (b1 == 'x' && b2 == 'x' && b3 == 'x') { breakloop = true; }
     if (c1 == 'x' && c2 == 'x' && c3 == 'x') { breakloop = true; }
     if (a1 == 'x' && b2 == 'x' && c3 == 'x') { breakloop = true; }
     if (a1 == 'x' && b1 == 'x' && c1 == 'x') { breakloop = true; }
     if (a2 == 'x' && b2 == 'x' && c2 == 'x') { breakloop = true; }
     if (a3 == 'x' && b3 == 'x' && c3 == 'x') { breakloop = true; }
}

int main()
{
    while (exitg < 2)
    {
    graphics();
    cout << "Your turn. Enter X coordinate (letter): ";
    cin >> x;
    system("cls");
    cout << "Enter Y coordinate (number): ";
    cin >> y;
    system("cls");
    game();
    //graphics();
    test();
    if (breakloop == true)
       {
       break;
       }
    }
   cout << "You won!";
   
   int thing = 5;
   cin >> thing;
}




Looks like I'm getting an error here. Your int graphics() doesn't return a value. Changing it to void makes it work tho ^^.
Also, why did you put a CLS after entering the X coordinate? =(

Also, I think you can't call this a TicTacToe game, you really should add a 2nd player. But, I looks fine this far.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: tic tac toe

Postby ShadowTH on Sun Oct 04, 2009 11:51 pm

Kool ty
ShadowTH
 
Posts: 6
Joined: Tue Aug 18, 2009 7:39 am


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests