tic tac toe against computer and another person

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.

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

ladyiouAuthor Commented: 2003-11-19

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.

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

ladyiouAuthor Commented: 2003-11-19

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

ladyiouAuthor Commented: 2003-11-19

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.

(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

smallbug005Commented: 2003-11-20

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

0

ladyiouAuthor Commented: 2003-11-20

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.

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] = ' ';
}

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:

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

ladyiouAuthor Commented: 2003-11-23

sorry that i took so long for me to get back to you but back to game, where would i find printBoard?