Solved

tic tac toe against computer and another person

Posted on 2003-11-19
14
1,492 Views
Last Modified: 2010-04-07
I DONT HAVE ENOUGH POINTS BUT THIS QUESTION IS MAJORLY IMPORTANT AND WHOEVER ANSWERS IT GETS THE MAJORITY OF MY POINTS.

How do i write a code for a two player game of TicTacToe.  The first few lines of this program should create a 3X3 two dimensional character array and should initialize all of the elements of the array to some character other that ‘X’ or ‘O’.  Following the initialization, the program should proceed as follows:

Draw the initialized gameboard

LOOP

Prompt player one to enter a move and test its validity

(i.e. the row and column that they want to place their marker in 1 1 or 1 3 or 2 3 or …)

Draw the modified game board

Determine if player one has won

If player one did not win and game is not a tie

Prompt player two to enter a move and test its validity

Draw the modified game board

Determine if player two has won

If player two did not win and game is not a tie, go to LOOP

Separate functions should be used to draw the gameboard, check if the player has won and to get and test moves from the users.
 Then how do i provide a modified version of this program that allows the user to play against the computer. I want to  include at least one additional function, which will implement the computer’s turn.  In addition to the code, I want to include a brief paper describing the heuristic that I am using to identify the computer’s next move.

0
Comment
Question by:ladyiou
  • 7
  • 6
14 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9784042
Could this be HOMEWORK?

We are not supposed to give you CODE or direct assistance for things you will turn in for credit (academic dishonesty would result if you turned it in as your work). We can and are happy to assist you in writing your own solution. That said, your assignment fails to answer a major question:

What programming language are you working in? What do you know how to do? Can you clear the screen and change the colors of characters? Do you know how to read a key from the user? If not, do you know how to read an integer from the user? How do you declare variables? Have you heard of arrays?

We need to know what language you're working in and you need to show us how you have gotten started (maybe draw the board on the screen if nothing else).

Good luck,
-bcl
0
 

Author Comment

by:ladyiou
ID: 9784376
I am working in C and no its not school work or anything my friends and I are trying to figure out how to write different codes to the game as a learning challenge based on fun for us.  i have heard of arrays yes, but dont know how to do anything more than use a simple printf statement to print my info and hello world. i have yet to get started. i was hoping that i could get some help on this by the weekend say saturday or sunday if anyone is interested.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9784445
(1) You can declare variables, right?

What variables are you going to need. Stop, think about this for a second. _WHAT_ do you need to keep track of. Imagine you weren't allowed to remember anything in your head and you needed to facilitate tic-tac-toe between a couple of your friends. What would you have to write down? You can assume that you can remember the RULES of the game, just not the state of the game.

Part of the state of the game is the board and its contents. How many squares are there on the board? How many DIFFERENT values could each square hold? When facilitating, your friends hand you a move, say (3, 3). Assuming the move is legal, what mark should you put on the board? Looks like we have another peice of state you need to keep track of.

Now, how would you declare variables to hold each of the above pieces of state? You'll also need variables for the row and column the user enters.

(2) You can write functions, right?

You will need, at a minimum, a routine to check whether someone has won given a board configuration, a routine that checks whether or not a move is legal (are the numbers between 1 and 3 and is the spot empty), a routine to display the board, and a routine to get a move from the user. Oh, and perhaps one to see if the game is a draw.

I hope this helps.
-bcl

PS - I am skeptical about friends including papers describing "heuristics" in their "learning challenges".
0
 

Author Comment

by:ladyiou
ID: 9784530
yes to all questions but i dont know the answers and yes the paper part is part of the challenge and like i said before it is not homework, if it were i would have left the site after not getting an immediate response. i dont know how to do anything other than what i said before so thats why i am asking help and it would be nice to get some help by the time above.  by the way i keep saying that it is not homework because i am 42 yrs. old and its just a game to my friends and i. some of who are programming illiterate(like me) and those who did go to school. not trying to sound mean or anything like that its just that its not nice to imply things.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 130 total points
ID: 9784615
I tend to ask questions in an effort to help you toward your goal.

What types (for variables) do you know?  I assume int and you say you've seen arrays. What else?

So, what variables are we going to need? How about a board and a current player? That keeps our state as minimal as possible.

How many squares are there on the board?
How many values can each have?

Similar question for current player: How many different values can current player have?

The answers to these questions will permit us to decide what C types to use to represent them for the computer.
This is the spot to start.

-bcl
0
 

Author Comment

by:ladyiou
ID: 9784649
i guess there will be any variables that will make the program work, as far as values go i truly dont know, i will be using pico as to write program and pine to compile and execute.  dont know much about C so please help the best way you can. it is a 3x3 game board ( i guess with 9 squares). i hope this helps you out some.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9786871
(1) As I said above, I try to pompt you with questions. So there are 9 elements on the board. That is a good start. We can decide how to express them as one unit. That is, it would be possible to use nine unrelated variables (we'll say they are int for the momemt) as in:

    int a, b, c, d, e, f, g, h, i;

But then how do we print them out? It takes a separate print statement for each variable. How do we translate row/column (the user's input) into a variable (for validating a move and for making a move)? No, this is too hard.

You mentioned knowing what an array is. So we COULD declare an array of variables (int again) to hold the values. Then one name would express all of them:

    int board[9]; // declares 9 int variables.

Now we need to be able to print it out and translate user input into indices into the array (so (1, 1), for example is the upper left corner or board[0] and (3, 1) is the lower left corner corresponding to board[6]). This requires mapping the squares to indices:

    0  1  2
    3  4  5
    6  7  8

So we know the size of the array. Note that it would be possible to use a two dimensional array (three rows by three columns) but that is probably more advanced than you want to get.

(2) This is a question about your knowledge: Given the variable h declared in the first variable list above, how would you print it on the screen? There are two fundamentally different ways to print in C/C++ and I want ot know which one you are used to.

(3) The question about values, what values a square in the board can take, was intended to get you thinking. When you draw a game on a piece of paper, what can each square contain? It starts the game with nothing in it so one value it can contain is "empty". You, playing X, could move into the square on any turn so it can contain an "X". I, playing O, could also move into it on any turn so it can contain an "O". Each square could contain any one of three values at any given moment.

Similarly, the number of values for currentMove is two: either it is X's turn or it is O's turn.

(4) Do you know what a char is? How to declare a character variable? How to print a character variable?

(5) Have you ever written a function on your own, other than main()? Do you know how to return a value from a function and do you know how to pass parameters to a function?

Note that if you are beginning with C++, Bruce Eckel's book _Thinking in C++ 2ed_ is available on-line: http://mindview.net/Books/DownloadSites  More C/C++ resources can be found by searching the C or C++ areas here at EE for "beginner resources" (lots of pointers).

Remember, I am trying to engage you in the creative process here, not just provide you with an answer. That is the whole point of learning on your own, the learning part.

Hope this helps, -bcl
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:smallbug005
ID: 9788322
do you have the book DataStructures in C and C++ by Andrew S Tanenbaum.
It will take you where you want.
>smallbug
0
 

Author Comment

by:ladyiou
ID: 9794863
thanks for all the help; the only print statement that i know is the printf statement and i guess the variables to get X and O to appear in the appropriate box when prompted would be to give X the value of 1 and O the value of 2 and have it print out that way (X & O) instead of the numbers.  no i do not know what char is and how to print  and the answer is no to question 4.  the only book that i have is by Deitel & Deitel and the title is C how to program introducing C++ and Java but it doesnt really help me.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9796523
Cool. printf was my introduction to C output and it is quite possibly easier to print a board with printf.

So you like 1 and 2 for X and O. That is okay BUT I want to make the board as easy to print as possible. So I am going to give you a one minute course on characters. You may know what I am about to say. Humor me and read it and I promise not to go too long.

Your computer is a binary digital computer. What does that mean? Binary means that internally it can only represent two values, 0 and 1 or off and on. Digital means it can only represent whole numbers (consider the difference between a stereo with 10 volume buttons and a volume knob; the buttons, labeled 1 to 10, permit you to choose one of ten whole values (digital) where the knob has infinitely many positions between the lowest (marked 1) and the highest (marked 10) (this is known as analog)). Why 0 and 1 and only whole numbers? It is easier (and cheaper and more reliable) to make circuts that can tell two states apart.

Okay, if the computer only knows off and on why is it so much more expensive than a light switch?  Because it has lots and lots (or, as Carl Sagan would say, "billions and billions") of switches. It can set them and come back later and read their settings. That is what the memory in your computer is (where your C variables are stored...knew I could bring this back to the situation at hand). Now, your computer doesn't know anything about what those patterns of 0s and 1s MEAN. Interpretation depends on the program and, ultimately, the user. Thus whether the switches at a given location represent the number 65, the character 'A', or an instruction to the computer to restart is completely up to the program running on the computer. How can a 0s and 1s MEAN anything? Well, encodings have been established to permit groups of switches, known as binary digits or bits, in sizes of 8 (a byte) or 16 to represent individual characters. You don't need to know anything about the encodings. You just need to know how to tell C to use one for you.

One more analogy and we will return to programming. Since any byte can contain a character or a number or an instruction, you have to tell the computer how to interpret the contents. You do this when you declare the type of a variable. When you say int the contents will be treated as a number; when you say char the contents will be treated as a single character. This is like an international post office. Each box in the office has a number just as each byte in your computer has a numeric address. When I rent a box, one of those numbers is labeled with my name and when you declare a variable called x one of those addresses is labelled with an x. If a postcard bearing the word "fern" were placed in the box with my name. What does it mean? That depends on the language I speak. In German it says "far" and in English it is a type of plant. So, whoever left it there for me either knew how I would interpret it or, quite probably, made a mistake in putting that value in the box. When you store a value in a variable, you need to know how it will be interpreted. Luckily the type is part of the declaration in C.

Well, hopefully that wasn't too bad. We want to be able to store and display characters. A short sample program should help:

#include <stdio.h>
int main()
{
  char ourChar;
  ourChar = 'X';
      /* Notice the single primes; a single charcter uses
         single primes; double quotes are used for strings
         which are stored as arrays of char; the peculiarities
         of strings are beyond the scope of this example */

  printf("the one character is %c \n", ourChar);

  ourChar = 'O';
  printf("the one character is %c \n", ourChar);

      /* We can tell the computer to interpret the contents of
         a memory location as we require without regard for
         what is stored there. I want to see 'O' as a number,
         as an int. Note that I didn't change the value stored,
         just the interpretation */
 
    printf("the one character's numeric value is %d", (int)ourChar);  
}

So now you know a little bit about characters. Imagine that we used a char to keep track of each board location. We could use 'X' and 'O' for when the spaces are occupied and ' ' (a space between the primes) when the spot is empty. So an array of characters would make a good board representation because it would be easy to print and easy to use. Lets see a couple of functions for using the character array:

/* set every element in the board to empty (a space). Uses a
    for loop to index each of the 9 elements in the array. Remember
    that they are numbered from 0 to 8 (count them, there are nine
    different values */
void clearBoard(char board[9]) {
    int i;
    for (i = 0; i < 9; ++i)
        board[i] = ' ';
}

void printBoard(char board[9]) {
    printf("%c | %c | %c \n", board[0], board[1], board[2]);
    printf("%c | %c | %c \n", board[3], board[4], board[5]);
    printf("%c | %c | %c \n", board[6], board[7], board[8]);
}

So, there are two simple routines for clearing and printing the board. Try your hand at it: write a simple function that takes a board, a character for who's turn it is (that is an 'X' or an 'O') and an index (one of the boxes from 0-8) and makes the move. Lets call it move:

void move(char board[9], char turn, int square);

You provide the body for the function.

The output of the following:

int main()
{
    char board[9];
    clearBoard(board);
    move(board, 'X', 4);
    move(board, 'O', 0);
    move(board, 'X', 1);
    move(board, 'O', 7);
    printBoard(board);
}

would then be

O | X |  
   | X |  
   | O |    

Hope this gets you thinking. Ask any questions you have. In case it wasn't obvious, I love teaching this stuff. Love doing it too, so back to the bit mines.
-bcl
}
0
 

Author Comment

by:ladyiou
ID: 9798504
okay i get a little bit of what you are trying to say. you just dont know how much i appreciate it.  okay now what function/code would i use to get the board to print out like this:

                         ****************
                         *        *        *        *
                         ****************
                         *        *        *        *
                         ****************
                         *        *        *        *                
                         ****************

connected or lines connected together like you would do if you were drawing on paper?

                     
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9799012
Look at the printf functions in printBoard. That is where all the action is for printing the  board. Since your template has 7 lines, if we stick to 1 line per printf (the '\n' at the end of each format string signifies the end of line character and starts a new line) you will need 7 printf lines. 4 of them are identical (first, third, fifth, and seventh line are all stars). The three lines in there now are the second, fourth, and sixth line (or rather are with some minor tweaking in the format string).

-bcl
0
 

Author Comment

by:ladyiou
ID: 9806605
sorry that i took so long for me to get back to you but back to game, where would i find printBoard?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9807112
Two comments back (from this one) I presented a printBoard function. That was what I was refering to.

-bcl
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Creating graphics and animations for Android 4 770
Will Java Webstart launch my game? 6 127
dividesSelf challange 15 79
copyEndy  challenge 15 55
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now