void check() { if (b[0].Text == b[1].Text && b[1].Text == b[2].Text && b[0].Text != "")// { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[3].Text == b[4].Text && b[4].Text == b[5].Text && b[3].Text != "")// { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[6].Text == b[7].Text && b[7].Text == b[8].Text && b[6].Text != "")// { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[0].Text == b[3].Text && b[3].Text == b[6].Text && b[0].Text != "")// { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[1].Text == b[4].Text && b[4].Text == b[7].Text && b[1].Text != "")// { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[2].Text == b[5].Text && b[5].Text == b[8].Text && b[2].Text != "")//6 { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[0].Text == b[4].Text && b[4].Text == b[8].Text && b[0].Text != "")//7 { MessageBox.Show(b[0].Text + " wins"); reset(); } if (b[2].Text == b[4].Text && b[4].Text == b[6].Text && b[2].Text != "")//8 { MessageBox.Show(b[0].Text + " wins"); reset(); } }
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

Shorter code for tic-tac-toe game code
Started by aazamsajid, Apr 08 2012 11:50 AM
algorithm shortcode tic tac toe tic-tac-toe
5 replies to this topic
#1
Posted 08 April 2012 - 11:50 AM
i am working on a tic tac toe game this is loop code ....how can write this in short way like in algorthm or arrays etc
#2
Posted 08 April 2012 - 12:42 PM
Here is your shortie :
Bool checkForWin( int c1, int c2, int c3 ) { if (b[c1].Text == b[c2].Text && b[c2].Text == b[c3].Text && b[c1].Text != "") { return true; } return false; }
void check() { if ( checkForWin( 0,1,2 ) || checkForWin( 3, 4, 5 ) || // put here all the combinations ) { MessageBox.Show(b[0].Text + " wins"); reset(); } }
#3
Posted 08 April 2012 - 12:43 PM
static bool is_Row() { int CurX; int CurY; for(CurY = 0; CurY < 3; CurY++) /* Rows */ if(b[CurY*3].Text == b[1 + CurY*3].Text == b[2 + CurY*3].Text) return true; for(CurX = 0; CurX < 3; CurX++) /* Columns */ if(b[CurX].Text == b[CurX + 1*3] == b[CurX + 2*3]) return true; if(b[0].Text == b[1 + 1*3].Text == b[1 + 2*3].Text || b[3 + 2*3].Text == b[2 + 2*3] == b[1 + 1*3]) return true; return false; }
void check() { if(is_Row()) { MessageBox.Show(b[0].Text + " wins")); reset(); } }
#4
Posted 08 April 2012 - 12:58 PM
Is_row does not work for diagonals, you will get index out of bounds error :
this should be the correct version of is_row "
this should be the correct version of is_row "
static bool is_Row() { int CurX; int CurY; for(CurY = 0; CurY < 3; CurY++) /* Rows */ if(b[CurY*3].Text == b[1 + CurY*3].Text == b[2 + CurY*3].Text) return true; for(CurX = 0; CurX < 3; CurX++) /* Columns */ if(b[CurX].Text == b[CurX + 1*3] == b[CurX + 2*3]) return true; if(b[0].Text == b[4].Text == b[8].Text || b[2].Text == b[4] == b[6]) return true; return false; }
#5
Posted 08 April 2012 - 07:22 PM
This is the beauty of open source and free software.
#6
Posted 09 April 2012 - 10:45 AM
when i apply is_row method this error is occrng.
An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.b'
An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.b'
Also tagged with one or more of these keywords: algorithm, shortcode, tic tac toe, tic-tac-toe
General Forums →
General Programming →
Learn algorithms and programming conceptsStarted by johnnylo, 23 Apr 2019 ![]() |
|
![]() |
||
General Forums →
General Programming →
Automated internet search algorithmStarted by Zainab, 12 Dec 2016 ![]() |
|
![]() |
||
Language Forums →
C# →
Tic Tac Toe with Local Multiplayer function added link problemStarted by googooli86, 20 Mar 2015 ![]() |
|
![]() |
||
Language Forums →
Java →
Tic Tac Toe Server Client bug.Started by pierlopre, 09 Apr 2014 ![]() |
|
![]() |
||
General Forums →
General Programming →
Help to write program for Algorithm (movie file compressor)Started by algorithm , 22 Mar 2014 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download