Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program that allows two people to play a game of Tic-Tac-Toe. The rules are asfollows The game is played on a 3x3

Write a C program that allows two people to play a game of Tic-Tac-Toe. The rules are asfollows The game is played on a 3x3 grid There are two players, each player is assigned either an X or O token Players take turns placing their tokens into empty squares The first player to get 3 tokens in a row (horizontally, vertically, or diagonally) is the winner If no player gets 3 tokens in a row, the match is a draw Required functionality: 1. All the rules listed above must be implemented. 2. If a player selects a square which is already occupied, the program should inform them thenspace is filled and require that they indicate a new space. 3. As soon as a player gets 3 tokens in a row, they should be declared the winner and the gameshould end. In other words, dont wait until all nine moves have been made to determine the winner. 4. The program should operate in a loop, asking the users if they would like to play again after thegame is over 5. Any accesses to arrays should be done with pointers and pointer arithmetic ONLY. There should be NO square brackets used when indexing into any arrays. No counters should be used for looping over arrays. You should loop by using pointer arithmetic. 6. You must modularize your program by breaking it up into functions. Some examplefunction names are a. playGameb. printBoardc. checkForWinner Extra credit functionality: 1. (2pts) If the users decides to play another game, the loser of the previous game should be thefirst to move. However, if a draw was reached, the first player to move should be the player who moved second in the previous game. 2. (8pts) Generalize the size of the board to be NxN. In order to win, a player must get N tokensin a row. The board size should be set with a #define N at the top of yourfile. Your source files should also have the name tictactoe_n.c a few moves later . a few moves later Input/Output Requirements: 1. Input of player moves (i.e. choosing where to put a token) should follow the row and columnformat shown in the example above. The user should input them one at a time. Rows andcolumns should be zero ordered; for a standard 3x3 board, this means valid inputs are [0,1,2]. 2. The outcome of the game should be printed exactly as shown in the example. The printf formatstrings for these are a. " **** Player %d wins! **** " b." **** The game has ended in a draw **** " Valid player numbers are 1 and 2 3. When asking the user if they would like to play again, the only options should be y and n4. You must display the current player to move, and do so exactly as shown in the example. The printf format string for this is " Current move: Player %d " This is important for validating an implementation of extra credit feature #1 5. If implementing extra credit feature #2 a. You must define the board size at the top of your file with #define N b. You must name your source file tictactoe_n.c so that we know you haveimplemented it Please do NOT USE memory allocation, and USE function declaration. This is my program:

#include #include #define N 9 int gameboard(); int gameboard() { int b[N], *p; for (p = b; p < b+ N; p++) *p = ' '; printf(" **** Tic-Tac-Toe **** "); printf(" %c | %c | %c ", b[0], b[1], b[2]); printf("---+---+--- "); printf(" %c | %c | %c ", b[3], b[4], b[5]); printf("---+---+--- "); printf(" %c | %c | %c ", b[6], b[7], b[8]); return 0; } int main(void) { int b[N], count = 0, player; char letter; while (count <= 9) { int r; printf("Enter spot: "); scanf("%d", &r); if(r> 9|| r < 1) { printf("invalid chose a spot from 1-9 "); } else if(b[r-1] =='X' || b[r-1] == 'O') { printf("The spot is taken, try again! "); } else if (count % 2 == 0) { letter = 'X'; player = 1; } else { letter = 'O'; player = 2; } b[r - 1] = letter; count++; gameboard(); printf("Current move: Player %d ", player); } return 0; }

can somebody please help me complete it with the given specs please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions