Monday, January 30, 2012

Hello guys. Do any of all know how to do this, Please give me a good amount of information so that i can under?

Lab 13 ( Chapter 8 Material )

As you no doubt know, computers are great for playing games. In this lab, we’ll write a program that plays a simple game of tic-tac-toe, a game you’ve probably played since childhood. We’ll write the program in such a way that it will allow one person to play against another. A more difficult program would allow a person to play against the computer, and the program would endow the computer with some kind of “intelligent” decision-making ability at each move. A REALLY sophisticated program could actually “learn” to make good strategic plays by evaluating the result of each move, using the initial board configuration and the configuration after the move has been made. That’s one kind of thing studied in the area of “artificial intelligence”. This kind of approach has been taken to construct, for example, expert chess-playing programs. In this class, we’ll stick to playing tic-tac-toe, and just relegate the computer to the task of recording and reporting the progress of the game.

Problem Statement

We need a program that will allow two players to play a game of tic-tac-toe. The program should ask for moves alternately from player X and player O. The program should display the game position as follows:

1 2 3

4 5 6

7 8 9

The players should enter their moves by entering one of the position numbers 1 through 9. The program should convert the specified move to one of the positions in the two-dimensional array shown above. The program should also validate the player’s desired move, to make sure the position hasn’t been previously occupied. After each move, the program should display the current configuration of the board and wait for a player to press the ‘enter’ key before continuing. A sample configuration is shown below, after 4 moves have been made. The program should make provision for a user’s terminating the game, if he wishes to do so, when it is his turn to play.



X X O

4 5 6

O 8 9



Hello guys. Do any of all know how to do this, Please give me a good amount of information so that i can under?
Well, I'm surely not going to write the code for you - partly because I have ethical problems with that and partly because you didn't indicate the language you're using. However:



Declare or instantiate an array - you could do it as 1x9 or 3x3, although conceptually it'll be easier with a 3x3. Initialize it with all 0's to start. As the game progresses, you're going to fill the array in. For the sake of argument, let 1 = X and 4 = O. (There's a reason I'm not going with 1 and 2 - stay with me here.)



To get the move into the right space, I'd use a look-up table. Essentially, you're going to hard-code in the fact that position 1 means row = 0 and column = 0. Position 2 means row = 0 and column = 1 and so on. I'd suggest using a switch or select case statement right after the input is validated, but that (again) depends on the language.



Once you have the position translated, it's easy to check to see if it's a valid move. If the appropriate location in the array is not 0, then it's an invalid move. Tell the user to try again. I'd use a posttest loop for that, something along the lines of a do...while.



Once a valid move has been made, check to see whether there is a winner. You might create another array with eight elements, each corresponding to one of the ways of winning. (3 horizontal, 3 vertical, and two diagonal). Each element in the second array contains the sum of the elements corresponding to a win in the first array. If any element ever attains a value of 3 (three x's) or 12 (three o's), then the game is over and you can declare a winner. You'll need to update the second array every time a valid move is made and then check it to see if there's a winner. This, by the way, is why I went with 1 and 4. If it were 1 and 2, you could get a row sum of 3 by having one x, one o, and one blank space. With 1 and 4, there's no way to get 3 or 12 without somebody winning.



As to allowing the user to quit prematurely, have them enter a 10 to leave, and check that when you validate your input.



If you want to get VERY fancy, have them enter a 'q' to quit. That means you'll have to parse the input a little more stringently, and may not be worth your effort.



Hope this helps.

No comments:

Post a Comment