• 0

[c++] Game


Question

i got board today and made a tictactoe game in c++

the basic of the game is an array

int Set[3][3];

and the elements have values 0 for empty 1 for player 1 and 2 for player 2

the computer will be player 2 and i would like to wite a NP script the game works great with 2 human players

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

You're on the right track, however I don't see your question. :huh:

Post some code you have already written and we will try to help you from there, as no one is going to write the whole thing for you.

bwx

Link to comment
Share on other sites

  • 0

From what I understand, you're trying to write some decision-making code for the computer to act as a player.

For a tic-tac-toe game this is pretty easy, you just need to check for empty spaces and which spaces would be smart to move into. You can do this with a couple of simple control path's. Assuming player 2 is the computer:

if (board[0][0] == 2)
{
    if (board[1][1] == 2 && board[2][2] == 0)
    { /* move to board[2][2] */ }
    else if (board[1][0] == 2 && board[2][0] == 0)
    { /* move to board[2][0] */ }
    /* ... */
}
else if (board[0][0] == 1)
{
    if (board[1][1] == 1)
    {
        /* block player 1 from winning */
        /* move to board[2][2] */
    }
    else if (board[1][0] == 1)
    { /* move to board[2][0] */ }
}

This is just a small example, but it should give you an idea on what path to take.

Another problem with these is usually that they always make the best move possible which makes it impossible to win by a human player.

If you have any questions on this feel free to ask and I'll try to elaborate.

bwx

Link to comment
Share on other sites

  • 0

omg, im working on a c++ tic tac toe game as well... with two players... 0 is empty, 1 is player 1, 2 is player 2..... and my board array is called 'set'

extremely weird coincidence. Anyway, im no where near putting AI in, im still working out a good system of checking matches. Ill attach my code, and you can see the way ive done things, if you have any interest to. Excuse the simpleness and extremely poor coding style, its a learning experience. :)

Anyway, for the computer player script i have some ideas. The simplest, but least effective way would just be to make the computer go in a random un occupied place. What i am planning to do is to program in all the well known strategies (there is only a select few possible) and then make the computer go random for the first shot, then follow the strategy for the rest of the shots. To make it easy, the computer would be random for the first two shots, etc.

I havent worked out how to put these ideas into effect, but i think these are effective ideas to make a AI player seem smart. :)

nac.cpp

Link to comment
Share on other sites

  • 0

The problem with implmenting A.I into Tic Tac Toe is that it's very easy to make a NPC that is unbeatable and without effort. like all A.I. it's only as smart as the programmer, but of course, with something as simple as Tic tac toe, the A.I won't be that difficult. Certainly not the best game to implement an A.I. unless you make it flawed through randomization, but it is a start.

Keep it up and continue moving onto bigger and better projects :)

Link to comment
Share on other sites

  • 0

YEAH, an unbeatable AI is boring. If you want it to be beatable, do a simple 'block then random' script.

what a funny co-incidence, i just finished a tictactoe game on the TI83. Unfortunately, my array was called [J], not set.

Link to comment
Share on other sites

  • 0

For the ai, make the computer go in a priority of stopping the opponent and then finishing his own tic tac toe. Make a system where

num = rand(1, 10); 
if (num <= 3)
do something smart;
else
do bullcrap;

for varying difficulties

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.