Results 1 to 5 of 5

Thread: X & O game

  1. #1
    Join Date
    Nov 2004
    Posts
    23

    Unanswered: X & O game

    Two players use a grid of nine spaces set out in 3 rows and 3 columns. Each player takes a turn making an X or an O (dependent on the players’ selection of a symbol) in one of the nine spaces. The first player to get three of their symbols in a horizontal, vertical or diagonal alignment wins. If this does not happen by the time the nine spaces are filled the game is squashed (no one wins).

    I would like to:
    a) Write an algorithm to solve the problem.
    b) The pseudo code which corresponds to the algorithm

    Any help is appreciated.....I'm at a complete lost how to start.


    Many thanks.

  2. #2
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    I did a similar assignment fora vb course that i did
    think about the logic involved, what needs to happen for a win to occur
    How would we check this
    how many combinations lead to a win etc.

  3. #3
    Join Date
    Feb 2005
    Posts
    333
    Sounds like someone has homework that is due.

  4. #4
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    You'd need a front end to enable players to enter their X or O, for instance with 9 text boxes, & locking a text box after data is entered...

    Data could be stored in 3x3 array, & after each entry this could checked to see if there was a win.

    If we refer to array as grid(x,y), then cells would be grid(1,1), grid(1,2), grid(1,3), grid(2,1) etc

    A simple way would be to check for win would be to set up test function to check if 3 grid values are the same & contain X or O:


    if grid(1,1) is not empty
    catkins

  5. #5
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    whoops, ran out of time or space:
    function check(val1,val2,val3)
    if (val1="X" or val1="O") and val1=val2 and val2=val3 then
    check = true
    else
    check = false
    end

    then call function for each possible line in grid:
    if check(grid(1,1), grid(1,2), grid(1,3) is true then
    we have winner
    if check(grid(1,1), grid(2,2), grid(3,3) is true then
    we have winner
    etc for all possible 8 lines

    Not that I encourage people doing too much of other's homework...

    Hope it helps

    Colin
    catkins

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •