View previous topic :: View next topic |
Author |
Message |
educofu Expert Cheater Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Thu Nov 25, 2010 11:03 am Post subject: Tic Tac Toe! (code + exe) |
|
|
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 |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Nov 25, 2010 4:15 pm Post subject: |
|
|
hi there is this cool new thing it's called loops they are kind of nice you should try them
|
|
Back to top |
|
|
HomerSexual Grandmaster Cheater Supreme Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Nov 25, 2010 4:23 pm Post subject: |
|
|
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%
_________________
|
|
Back to top |
|
|
Jorg hi I post too much Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Sat Nov 27, 2010 4:21 pm Post subject: |
|
|
Hahha lol this thing looks like junk!
_________________
CEF will always stay alive. |
|
Back to top |
|
|
92Garfield I'm a spammer Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sat Nov 27, 2010 4:50 pm Post subject: |
|
|
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 |
|
|
Jorg hi I post too much Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Sun Nov 28, 2010 7:07 pm Post subject: |
|
|
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 |
|
|
92Garfield I'm a spammer Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sun Nov 28, 2010 11:22 pm Post subject: |
|
|
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 |
|
|
Jorg hi I post too much Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Mon Nov 29, 2010 2:43 pm Post subject: |
|
|
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 |
|
|
92Garfield I'm a spammer Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Mon Nov 29, 2010 3:01 pm Post subject: |
|
|
yea, chess is "a lil" more complex.
_________________
|
|
Back to top |
|
|
educofu Expert Cheater Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Tue Nov 30, 2010 11:41 am Post subject: |
|
|
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 |
|
|
s0n1k How do I cheat? Reputation: -1
Joined: 05 Sep 2009 Posts: 3
|
Posted: Thu Dec 02, 2010 6:04 pm Post subject: |
|
|
Buena
|
|
Back to top |
|
|
|