7 Replies - 483 Views - Last Post: 05 December 2011 - 12:33 PM Rate Topic: -----

#1 PixelBit  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:13 AM

Ok so for computing class as an extra thing i was told to make a naughts and crosses game, first player v player and then a player v computer one.

Obviously no problems there with the player vs player one, but for the vs computer one, i want it to do these steps:

Check if in this current turn it can win, if it can win it will go ahead and win. (If true end of move)
If false check if opponent can win next turn, if they can block them. (If true end of move)
If flase determine which place would be the best place to use turn on... end of move

For these to happen i need to check if two out of three boxes are chosen (eg. out of box 1, 2 and 3, two of the boxes are taken) How can I do this?

Note: To check if a box is taken i do this:
If (btn1 = "O") OR (btn1 = "X")



If there isn't a way does that mean I'm going to have to go over every single combination of boxes? 'Cause that sounds like a complete pain in the ass.

Thanks in advance.

This post has been edited by PixelBit: 05 December 2011 - 09:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: If two out of these three statements are correct - execute code...

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8390
  • View blog
  • Posts: 31,200
  • Joined: 12-June 08

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:27 AM

How are you storing your tic-tac-toe board? An array? It should be a pretty trivial issue of looking at each square occupied (#occupy_square - sort of like #occupy_wallstreet!) and see if an available square is near... that's a simple index+1 or index-1 depending, again, on your storage.
Was This Post Helpful? 0
  • +
  • -

#3 PixelBit  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:33 AM

View Postmodi123_1, on 05 December 2011 - 09:27 AM, said:

How are you storing your tic-tac-toe board? An array? It should be a pretty trivial issue of looking at each square occupied (#occupy_square - sort of like #occupy_wallstreet!) and see if an available square is near... that's a simple index+1 or index-1 depending, again, on your storage.

Yes in an array there are 9 buttons and i have an array that refers to each button.

So I would like specify box 2 and do + or - 1 thats fine but what about boxes 1,5,9 or boxes 1,4,7?

This post has been edited by PixelBit: 05 December 2011 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:42 AM

Your use of Controls is confusing you.

Step back and just think about the basic pattern of grid like this
 0 | 1 | 2 
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8 


What is common pattern of the number;-
Going Vertically?
Going Horizontally?
For Each of the two diagonals?

This post has been edited by AdamSpeight2008: 05 December 2011 - 09:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8390
  • View blog
  • Posts: 31,200
  • Joined: 12-June 08

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:50 AM

Well think about it logically.. You have a two-d array, right? A venerable string(3,3) set up, right? That means if you want to examine moves from (0,0) then You would need to check (0+1, 0), (0, 0+1), and (0+1, 0+1).

Everyone but the center square would be checking three squares! Repeat for them! Poor 1,1 has to check all the squares!

Sure you could probably programmatically make some complicate system up, but a switch statement and the hard coded increments would be much faster.


Quote

_(0,0)_ | _(1,0)_ | _(2,0)_
---------|---------|---------
_(0,1)_ | _(1,1)_ | _(2,1)_
---------|---------|---------
_(0,2)_ | _(1,2)_ | _(2,3)_

Was This Post Helpful? 0
  • +
  • -

#6 PixelBit  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 09:58 AM

View Postmodi123_1, on 05 December 2011 - 09:50 AM, said:

Well think about it logically.. You have a two-d array, right? A venerable string(3,3) set up, right? That means if you want to examine moves from (0,0) then You would need to check (0+1, 0), (0, 0+1), and (0+1, 0+1).

Everyone but the center square would be checking three squares! Repeat for them! Poor 1,1 has to check all the squares!

Sure you could probably programmatically make some complicate system up, but a switch statement and the hard coded increments would be much faster.


Quote

_(0,0)_ | _(1,0)_ | _(2,0)_
---------|---------|---------
_(0,1)_ | _(1,1)_ | _(2,1)_
---------|---------|---------
_(0,2)_ | _(1,2)_ | _(2,3)_


No I have 9 buttons and an array from 0-8 for each button. Anyway I wasn't looking for a system or anything, i was wondering if there was something built in that instead of looking for at leat one statement being true, or all, that there would be something that checks if two statements are true, i guess there isnt, thanks.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 10:12 AM

It's simple you use 1D.

Function CheckH(byval g(),s,p) as bool
  Return  {g(s+0),g(s+1),g(s+2)}.All(Function(x) x=p)
End Function
Function CheckV(byval g(),s,p) as bool
  Return  {g(s+0),g(s+3),g(s+6)}.All(Function(x) x=p)
End Function 
Function CheckH1(byval g(),s) as bool
  Return  {g(0),g(4),g(8)}.All(Function(x) x=p)
End Function 
Function CheckH1(byval g(),s) as bool
  Return  {g(2),g(4),g(6)}.All(Function(x) x=p)
End Function   

This post has been edited by AdamSpeight2008: 05 December 2011 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#8 Beach_Coder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 123
  • Joined: 10-November 11

Re: If two out of these three statements are correct - execute code...

Posted 05 December 2011 - 12:33 PM

When I need to know if two out of three things is this or that I usually do one of two things:
1) Check one if one is true, then check if either of the other two are true (using "or" if it's an either or both deal or "xor" if it's a one and only one of both).
or
2) If I am dealing with a loop, I'll declare an integer variable, then every time one thing or other become true, add 1 to that variable's value and have the loop be a Do Until x = 2 (if I am looking for 2 out of 3 for example) or a Do While x < 2 or some such.

If I am dealing with my loop situation but the Do WHile or Do Until isn't appropriate, whenever I need to check on the matter I might do something like:
Sub Even_Odd()


        Dim a As Integer
        Dim b As Integer
        Dim c As Integer

        a = 9
        b = 11
        c = 15

        If NumberofEvens(a, b, c) = 2 Then
            Console.WriteLine("Two out of three integers are even.")
        Else
            Console.WriteLine("At least two out of three integers are odd.")
        End If
        Console.ReadKey()

    End Sub
    Function NumberofEvens(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer

        Dim Number_Of_Evens As Integer = 0
        If x Mod 2 = 0 Then
            Number_Of_Evens = Number_Of_Evens + 1
        End If
        If y Mod 2 = 0 Then
            Number_Of_Evens = Number_Of_Evens + 1
        End If
        If z Mod 2 = 0 Then
            Number_Of_Evens = Number_Of_Evens + 1
        End If

        Return Number_Of_Evens

    End Function



Or in your situation, something like this might be useful:
 Sub Buttons()
        Dim btn1 As String
        Dim btn2 As String
        Dim btn3 As String

        btn2 = "X"
        btn3 = "O"

        If Two_Out_Of_Three(btn1, btn2, btn3) = True Then
            Console.WriteLine("Two boxes are taken.")
        End If

        Console.ReadKey()


    End Sub

    Function Two_Out_Of_Three(ByVal a As String, ByVal b As String, ByVal c As String) As Boolean
        Dim TwoBoxesAreTaken As Boolean

        If a = "" Then
            If b <> "" And c <> "" Then
                TwoBoxesAreTaken = True
            End If
        Else
            If b <> "" Xor c <> "" Then
                TwoBoxesAreTaken = True
            End If
        End If

        Return TwoBoxesAreTaken

    End Function





The above works for two of three only, not two of three for each contiguous three in a 3 x 3 box.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1