Results 1 to 8 of 8
  1. #1
    Shark23's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Location
    Kansas
    Posts
    424
    Reputation
    10
    Thanks
    55
    My Mood
    Cool

    Basic C++ Tic Tac Toe

    Hey all, I'm making tic tac toe in C++ and have a little trouble. I have some code but it isn't with me right now. The problem is, I have to make it very basic. I can only use if-then statements, cases, loops, and the iostream and string libraries since that is all we have done at school. I have the player's turn but need some help on the computer so it doesn't just pick a random number and go there. It needs to be at lease semi-intelligent. Any ideas?
    Assembly Programmer

  2. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    You could assign points.

    example:
    Your bot is o, and it's currently the bots turn.

    x.x
    ...
    .o.

    highlighted in bold is a spot where your opponent could place his x next turn to win, so lets upgrade that amount of points
    point table(most useful has highest number, 2 points are given for blocking a possible win for the opponent, and 1 point is given for opening up a win opportunity for yourself.)

    030
    010
    101

    so the bot picks the highest scored spot

    xox
    .x.
    .o.

    opponent places it in the centre...
    new scoretable:
    000
    000
    303

    ah, two times 3 points he wins!

    another example, where it would be a tie:
    x.x
    ...
    o..

    020
    000
    011

    xox
    .x.
    o..

    000
    000
    012

    xox
    .x.
    oxo

    000
    000
    000

    doesnt matter where it places it, coz it's a tie
    could assign 3 points to blocking opponent, 2 to opening up a win situation for yourself, 1 for every possible move and 0 for not possible.
    Last edited by Hell_Demon; 04-16-2010 at 05:35 PM.
    Ah we-a blaze the fyah, make it bun dem!

  3. #3
    Arhk's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    Engineering
    Posts
    3,618
    Reputation
    35
    Thanks
    217
    My Mood
    Amused
    My approach would be properly restricting movements (making sure I implemented the 3x3 restriction properly) and setting flags for positions by their directional type (naming all those spaces with proper variable to have set as flags)... And work out an (probably extremely simple in this case) algorithm for the computer to move on based on the setting of those flags. But while the flag and Algorithmic approach might seem more intelligent having your bot move according to how Hell_Demon describes is probably simple enough, plus it might be faster for a situation as simple as this.
    ~
    "If the world hates you, keep in mind that it hated me first." John 15:18

  4. #4
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    I would make it so that you never win, and the bot always causes a tie. cuz tic-tac-toe is too easy.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  5. #5
    Arhk's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    Engineering
    Posts
    3,618
    Reputation
    35
    Thanks
    217
    My Mood
    Amused
    A tie is inevitable isn't it, if they both play a perfect game.... I guess in that sense it's interesting, it would be impossible for success, his teacher must be on the dense side....
    ~
    "If the world hates you, keep in mind that it hated me first." John 15:18

  6. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by why06 View Post
    I would make it so that you never win, and the bot always causes a tie. cuz tic-tac-toe is too easy.
    +1 for opening up a move for yourself
    +2 for blocking the opponent of winning

    x.x
    ...
    o..

    020
    000
    011

    xox
    ...
    o.x

    000
    032
    010

    f*ck yeah, lost there's always a way to win as long as the center isnt taken by the time of your 2nd move.
    Ah we-a blaze the fyah, make it bun dem!

  7. #7
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by Hell_Demon View Post
    +1 for opening up a move for yourself

    f*ck yeah, lost there's always a way to win as long as the center isnt taken by the time of your 2nd move.
    Everytime I play tic tac toe I take the center first. (Instant draw)

    Now what's fun is That got old so I made up multiplayer tic tac toe, where u add an extra row and column for everyone playing. Now that's fun! YOu can make up strategies and everything, plus its impossible to defend against the first player urself since the board is so huge makes for some pretty interesting gameplay, because if u just choose not to help contain player 1, but instead go for the win you usually end up losing next round.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  8. #8
    Arhk's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    Engineering
    Posts
    3,618
    Reputation
    35
    Thanks
    217
    My Mood
    Amused
    why for that kind of stuff just play connect four!~
    "If the world hates you, keep in mind that it hated me first." John 15:18

Similar Threads

  1. [Tutorial]Tic Tac Toe Game[Leeched]
    By jimmy541 in forum Visual Basic Programming
    Replies: 7
    Last Post: 04-20-2010, 03:43 PM
  2. BLack Horseman Tic Tac Toe Game
    By aLcohoL_95 in forum General
    Replies: 15
    Last Post: 04-14-2010, 01:43 PM
  3. [Help] Tic-Tac-Toe
    By aLcohoL_95 in forum Visual Basic Programming
    Replies: 11
    Last Post: 04-08-2010, 10:04 PM
  4. Just owned some Tic Tac Toe :)
    By skip in forum C++/C Programming
    Replies: 1
    Last Post: 08-30-2008, 05:03 AM
  5. ARUN BEAT ME ON TIC TAC TOE
    By CAPTAIN OBVIOUS in forum Flaming & Rage
    Replies: 22
    Last Post: 04-30-2008, 05:33 PM