Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Tic Tac Toe! (code + exe)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
educofu
Expert Cheater
Reputation: 3

Joined: 21 Aug 2009
Posts: 171
Location: Brazil,MG,OP

PostPosted: Thu Nov 25, 2010 11:03 am    Post subject: Tic Tac Toe! (code + exe) This post has 1 review(s) Reply with quote

Heres my tic tac toe:

Code:

#include <iostream>
#include <cstdlib>
using namespace std;

void cs()
{system("CLS");}

int random (int a,int b)
{
    if (b<a) return 0;
    int good;
    random:
    int r=rand();
    if (r>=a) good=1;
    else goto random;
    if (r<=b) good+=1;
    else goto random;
    if (good==2)return r;
    else return 0;
}

int action;
int A1,B1,C1;
int A2,B2,C2;
int A3,B3,C3;

int your_turn();
int pc_turn();
int print();
int check();

int main()
{
    system("mode con cols=22");
    system("mode con lines=10");
    system("color 70");
    system("title Tic Tac Toe!");
    A1=0,B1=0,C1=0;
    A2=0,B2=0,C2=0;
    A3=0,B3=0,C3=0;
    cs();
    cout<<"\t_|_|_\n\t_|_|_\n\t | | \n";
    cout<<"\nWho starts?\n\n1-You\n2-Computer";
    cin>>action;
    if (action==1)your_turn();
    else if (action==2)pc_turn();
    else {cs();main();}
    }

int print()
{
    if(A1==1)cout << "\tX|";
    else if (A1==2)cout << "\tO|";
    else cout << "\t_|";
    if (B1==1)cout << "X|";
    else if (B1==2)cout << "O|";
    else cout << "_|";
    if (C1==1)cout << "X";
    else if (C1==2)cout << "O";
    else cout << "_";
    cout << "\n";
    if(A2==1)cout << "\tX|";
    else if (A2==2)cout << "\tO|";
    else cout << "\t_|";
    if (B2==1)cout << "X|";
    else if (B2==2)cout << "O|";
    else cout << "_|";
    if (C2==1)cout << "X";
    else if (C2==2)cout << "O";
    else cout << "_";
    cout << "\n";
    if(A3==1)cout << "\tX|";
    else if (A3==2)cout << "\tO|";
    else cout << "\t |";
    if (B3==1)cout << "X|";
    else if (B3==2)cout << "O|";
    else cout << " |";
    if (C3==1)cout << "X";
    else if (C3==2)cout << "O";
    else cout << " ";
    cout << "\n";
    }

int check()
{
    if((A1==1 && B1==1 && C1==1)||(A2==1 && B2==1 && C2==1)||(A3==1 && B3==1 && C3==1)||(A1==1 && A2==1 && A3==1)||
    (B1==1 && B2==1 && B3==1)||(C1==1 && C2==1 && C3==1)||(A1==1 && B2==1 && C3==1)||(C1==1 && B2==1 && A3==1))
    {
         cs();
         print();
         cout << "\nYou WIN! restart?\n(1 - Yes / 2 - No)";
         cin >> action;
         if (action==1)main();
         else exit(0);
         }
    else if((A1==2 && B1==2 && C1==2)||(A2==2 && B2==2 && C2==2)||(A3==2 && B3==2 && C3==2)||(A1==2 && A2==2 && A3==2)||
    (B1==2 && B2==2 && B3==2)||(C1==2 && C2==2 && C3==2)||(A1==2 && B2==2 && C3==2)||(C1==2 && B2==2 && A3==2))
    {
         cs();
         print();
         cout << "\nYou LOSE! restart?\n(1 - Yes / 2 - No)";
         cin >> action;
         if (action==1)main();
         else exit(0);
         } 
    else if(A1!=0 && A2!=0 && A3!=0 && B1!=0 && B2!=0 && B3!=0 && C1!=0 && C2!=0 && C3!=0)
    {
         cs();
         print();
         cout << "\nDraw! restart?\n(1 - Yes / 2 - No)";
         cin >> action;
         if (action==1)main();
         else exit(0);
    }
}

int your_turn()
{
    cs();
    check();
    print();
    int line;
    int colu;
    cout<< "\nLine?";
    cin>> line;
    cout<< "Column?";
    cin>> colu;
    if (line==1)
    {
              if (colu==1 && A1==0)A1=1;
         else if (colu==2 && B1==0)B1=1;
         else if (colu==3 && C1==0)C1=1;
         else
         {
              cout << "Invalid turn\n";
              your_turn();
              }
         }
    else if (line==2)
    {
              if (colu==1 && A2==0)A2=1;
         else if (colu==2 && B2==0)B2=1;
         else if (colu==3 && C2==0)C2=1;
         else
         {
              cout << "Invalid turn\n";
              your_turn();
              }
         }
    else if (line==3)
    {
              if (colu==1 && A3==0)A3=1;
         else if (colu==2 && B3==0)B3=1;
         else if (colu==3 && C3==0)C3=1;
         else
         {
              cout << "Invalid turn\n";
              your_turn();
              }
         }
    else
    {
         cout << "Invalid turn\n";
         your_turn();
         }
    pc_turn();
    }
   
int pc_turn()
{
    //completers
      if (A1==2 && C1==2 && B1==0){B1=2;your_turn();}
 else if (A1==2 && C3==2 && B2==0){B2=2;your_turn();}
 else if (C1==2 && C3==2 && C2==0){C2=2;your_turn();}
 else if (A1==2 && A3==2 && A2==0){A2=2;your_turn();}
 else if (A1==2 && B1==2 && C1==0){C1=2;your_turn();}
 else if (A1==2 && A2==2 && A3==0){A3=2;your_turn();}
 else if (A1==2 && B2==2 && C3==0){C3=2;your_turn();}
 else if (B1==2 && B2==2 && B3==0){B3=2;your_turn();}
 else if (B1==2 && C1==2 && A1==0){A1=2;your_turn();}
 else if (B1==2 && B3==2 && B2==0){B2=2;your_turn();}
 else if (C1==2 && B2==2 && A3==0){A3=2;your_turn();}
 else if (C1==2 && C2==2 && C3==0){C3=2;your_turn();}
 else if (A2==2 && B2==2 && C2==0){C2=2;your_turn();}
 else if (A2==2 && C2==2 && B2==0){B2=2;your_turn();}
 else if (B2==2 && A2==2 && C2==0){C2=2;your_turn();}
 else if (B2==2 && C2==2 && A2==0){A2=2;your_turn();}
 else if (B2==2 && A3==2 && C1==0){C1=2;your_turn();}
 else if (A3==2 && A2==2 && A1==0){A1=2;your_turn();}
 else if (A3==2 && B3==2 && C3==0){C3=2;your_turn();}
 else if (A3==2 && C1==2 && B2==0){B2=2;your_turn();}
 else if (B2==2 && C3==2 && A1==0){A1=2;your_turn();}
    //blockers
 else if (A1==1 && C1==1 && B1==0){B1=2;your_turn();}
 else if (A1==1 && C3==1 && B2==0){B2=2;your_turn();}
 else if (A1==1 && A3==1 && A2==0){A2=2;your_turn();}
 else if (A1==1 && B1==1 && C1==0){C1=2;your_turn();}
 else if (A1==1 && A2==1 && A3==0){A3=2;your_turn();}
 else if (A1==1 && B2==1 && C3==0){C3=2;your_turn();}
 else if (C1==1 && C3==1 && C2==0){C2=2;your_turn();}
 else if (B1==1 && B2==1 && B3==0){B3=2;your_turn();}
 else if (B1==1 && C1==1 && A1==0){A1=2;your_turn();}
 else if (B1==1 && B3==1 && B2==0){B2=2;your_turn();}
 else if (C1==1 && B2==1 && A3==0){A3=2;your_turn();}
 else if (C1==1 && C2==1 && C3==0){C3=2;your_turn();}
 else if (A2==1 && B2==1 && C2==0){C2=2;your_turn();}
 else if (A2==1 && C2==1 && B2==0){B2=2;your_turn();}
 else if (B2==1 && C2==1 && A2==0){A2=2;your_turn();}
 else if (B2==1 && A2==1 && C2==0){C2=2;your_turn();}
 else if (B2==1 && A3==1 && C1==0){C1=2;your_turn();}
 else if (A3==1 && A2==1 && A1==0){A1=2;your_turn();}
 else if (A3==1 && B3==1 && C3==0){C3=2;your_turn();}
 else if (A3==1 && C1==1 && B2==0){B2=2;your_turn();}
 else if (B2==1 && C3==1 && A1==0){A1=2;your_turn();}
    //order
 else{     if (C3==0){C3=2;your_turn();}
      else if (C1==0){C1=2;your_turn();}
      else if (A3==0){A3=2;your_turn();}
      else if (A1==0){A1=2;your_turn();}
      else if (B2==0){B2=2;your_turn();}
      else if (C2==0){C2=2;your_turn();}
      else if (B3==0){B3=2;your_turn();}
      else if (A2==0){A2=2;your_turn();}
      else if (B1==0){A2=2;your_turn();}
      }   
    }


link: http://ifile.it/dx3o05g

_________________
"I finally started thinking outside of the box, only to find myself in a larger box."
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Nov 25, 2010 4:15 pm    Post subject: Reply with quote

hi there is this cool new thing it's called loops they are kind of nice you should try them
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Nov 25, 2010 4:23 pm    Post subject: Reply with quote

slovach wrote:
hi there is this cool new thing it's called loops they are kind of nice you should try them


would shorten his code by about 80% Razz

_________________
Back to top
View user's profile Send private message
Jorg hi
I post too much
Reputation: 7

Joined: 24 Dec 2007
Posts: 2276
Location: Minnesota

PostPosted: Sat Nov 27, 2010 4:21 pm    Post subject: Reply with quote

Hahha lol this thing looks like junk!
_________________
CEF will always stay alive.
Back to top
View user's profile Send private message
92Garfield
I'm a spammer
Reputation: 57

Joined: 20 Dec 2007
Posts: 5871
Location: Banana Republic Germany

PostPosted: Sat Nov 27, 2010 4:50 pm    Post subject: This post has 1 review(s) Reply with quote

I got a function to check if player (p) wins in the field f which is a 2 dimensional array (isn't Tic Tac Toe the perfect presentation of a 2D array?)

It's the shortest code I could come up with.

Also Jorghi, shut up you don't know any coding at all.

Code:
function checkWin(p):Boolean {
   for (var i = 0; i < 3; i++) {
      if (f[i][0].owner == p && f[i][1].owner == p && f[i][2].owner == p) {
         return true;
      }
   }
   for (var j = 0; j < 3; j++) {
      if (f[0][j].owner == p && f[1][j].owner == p && f[2][j].owner == p) {
         return true;
      }
   }
   if (f[0][0].owner == p && f[1][1].owner == p && f[2][2].owner == p) {
      return true;
   }
   if (f[0][2].owner == p && f[1][1].owner == p && f[2][0].owner == p) {
      return true;
   }
   return false;
}

_________________
Back to top
View user's profile Send private message
Jorg hi
I post too much
Reputation: 7

Joined: 24 Dec 2007
Posts: 2276
Location: Minnesota

PostPosted: Sun Nov 28, 2010 7:07 pm    Post subject: Reply with quote

Shorter.

Code:
function checkWin(p):Boolean {
for (var i = 0;i<3;i++){
if (f[i][0].owner == p && f[i][1].owner == p && f[i][2].owner == p) {
         return true;
      }
 if (f[0][i].owner == p && f[1][i].owner == p && f[2][i].owner == p) {
         return true;
      }
 if (f[0][i].owner == p && f[1][1].owner == p && f[2][2-i].owner == p && i!=2) {
         return true;
      }
}
return false;
}

_________________
CEF will always stay alive.
Back to top
View user's profile Send private message
92Garfield
I'm a spammer
Reputation: 57

Joined: 20 Dec 2007
Posts: 5871
Location: Banana Republic Germany

PostPosted: Sun Nov 28, 2010 11:22 pm    Post subject: Reply with quote

haha, yea a lil.

But the last one wont do it:

0:
if (f[0][0].owner == p && f[1][1].owner == p && f[2][-2].owner == p) {
return true;
}

1:
if (f[0][1].owner == p && f[1][1].owner == p && f[2][-1].owner == p) {
return true;
}

2: (ignored in your code)
if (f[0][2].owner == p && f[1][1].owner == p && f[2][0].owner == p) {
return true;
}

So the diagonals are not checked but 2 irrelevant ones.

EDIT:

This would work for last if
Code:
   if (f[0][i].owner == p && f[1][1].owner == p && f[2][(i-2)*-1].owner == p && i != 1) {
      return true;
   }

_________________
Back to top
View user's profile Send private message
Jorg hi
I post too much
Reputation: 7

Joined: 24 Dec 2007
Posts: 2276
Location: Minnesota

PostPosted: Mon Nov 29, 2010 2:43 pm    Post subject: Reply with quote

Yeah I forgot the to fix that. Though I wonder how you could apply that concept to a chess game without using many if statements.

But I guess lines of code is a useless thing in programing because all that matters is the total number of operations and how much effort each kind requires to the computer.

Code:
function checkWin(p):Boolean {
for (var i = 0;i<3;i++){
if ((f[i][0].owner == p && f[i][1].owner == p && f[i][2].owner == p) || (f[0][i].owner == p && f[1][i].owner == p && f[2][i].owner == p) || (f[0][i].owner == p && f[1][1].owner == p && f[2][(i-2)*-1].owner == p && i!=2)) {
         return true;
      }
return false;
}

_________________
CEF will always stay alive.
Back to top
View user's profile Send private message
92Garfield
I'm a spammer
Reputation: 57

Joined: 20 Dec 2007
Posts: 5871
Location: Banana Republic Germany

PostPosted: Mon Nov 29, 2010 3:01 pm    Post subject: Reply with quote

yea, chess is "a lil" more complex.
_________________
Back to top
View user's profile Send private message
educofu
Expert Cheater
Reputation: 3

Joined: 21 Aug 2009
Posts: 171
Location: Brazil,MG,OP

PostPosted: Tue Nov 30, 2010 11:41 am    Post subject: Reply with quote

92Garfield wrote:
yea, chess is "a lil" more complex.


"a lil" > what a ironic euphemism.

just figured out some errors and ill try to fix it.

_________________
"I finally started thinking outside of the box, only to find myself in a larger box."
Back to top
View user's profile Send private message MSN Messenger
s0n1k
How do I cheat?
Reputation: -1

Joined: 05 Sep 2009
Posts: 3

PostPosted: Thu Dec 02, 2010 6:04 pm    Post subject: This post has 1 review(s) Reply with quote

Buena
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites