Public Group

# Tic-Tac-Toe Winning proof?

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

## Recommended Posts

So, I've spent a few minutes googling without turning anything really useful up, so... While I have my own idea of how to prove that someone has won the game, what are your ideas on proving a win?

##### Share on other sites
Proof? Three-in-a-row means the player wins. What is there to prove?

##### Share on other sites
I ment as far as storing and proving mathmatically/logicaly in programming. This IS a programming forum, so I sort of figured I wouldn't have to state that part of the question.

##### Share on other sites
"Tic Tac Toe Board (Gray is the actual board shown in the game)

Numbers: 0 1 2

0 4 9 2
1 3 5 7
2 8 1 6

By looking at the table above, you will initialize all of the numbers above in the multi-dimensional array. If you add the three numbers in vertical or horizontal or diagonal, the results will be 15. So, have a function to the do the calculation for that. Whichever player put the X or O in one of the slots in the table, you will then assign that number appropriately to that user and show the X or O in the table instead of number."

Maybe this will work for you :D

##### Share on other sites
Tic-tac-toe isn't a particularly big game. You could just search all possible moves from the current board position and verify that they all lead to a win for the same person.

##### Share on other sites
The board is 3x3, there are 8 winning positions for each side. Testing each individually isn't overwhelming.

##### Share on other sites
thats probably the simplest way to do it. Jsut check the 8 possibilities.

##### Share on other sites
Yeah, I was thinking just have checks for the 8 winnings, but was wondering if anyone has a 'cooler' way.

##### Share on other sites
I wrote this:

#include <iostream>#define C(v) cout<<(v?v==X?'X':'O':'.')using namespace std;enum P{N,X,O};P p=O,b[9],c;int d[]={0,3,1,3,2,3,0,1,3,1,6,1,0,4,2,2},t,*u;int main(){for(;;){y:for(t=0;t<9;++t)t=N;for(;;){z:for(t=0;t<9;++t)C(t)<<((t+1)%3?' ':'\n');p=p==X?O:X;for(;;){C(p)<<", enter your move (1-9): ";cout.flush();cin.sync();t=cin.get()-'1';t=t%3+3*(2-t/3);if(t>-1&&t<9&&!t)break;}t=p;for(u=d;u<d+16;u+=2)if((c=b[*u]))for(t=1;t<3;)if(c!=b[*u+t*u[1]])break;else if(++t>2){C(c)<<" Wins!\n";goto y;}for(t=0;t<9;++t)if(!t[b])goto z;cout<<"Draw!\n";goto y;}}}

It's a little known fact that by using as little white space as possible and using one character variable names your programs will run faster.*

*This is a lie, it in fact just makes it harder to read.

##### Share on other sites
Quote:
 Original post by smart_idiot*This is a lie, it in fact just makes it harder to read.

Actually it does make them smaller [wink]

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 96
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009641
• ### Who's Online (See full list)

There are no registered users currently online

×