[Solved] SOLVED:Intelligent Tic Tac Toe

30 $

File Name: SOLVED:Intelligent_Tic_Tac_Toe.zip
File Size: 282.6 KB

SKU: [Solved] SOLVED:Intelligent Tic Tac Toe Category: Tag:

Or Upload Your Assignment Here:


CPSC 120 Project #3— tic-tac-toe artificial intelligenceIntroductionIn this project you will write a C++ program that implements an artificial intelligence player for the game of tic-tac-toe.BackgroundTic-tac-toe, also known as noughts & crosses, is a two player game played on a 3×3 square grid.One player is called X and the other player is O. Players alternate placing their mark (X or O) in squares of the grid. The X player goes first. If a player makes a line of three horizontal, vertical, or diagonal marks, they win the game.Your objective is to create a computer player that makes the best move it can.Example 1: winning movesOne rule of thumb is that if you can win the game with one move, you should do thatimmediately. For example, in the following board,1X XOOX can win by taking the center cell in the top row:X X XOOExample 2: blocking movesIf there are no winning moves, but it’s possible to block the other playerfrom winning on their next turn, you need to block them to stay in thegame. Suppose the board looks like the following, and it is X’s turn:O XX OX has no winning moves, and O could win by playing in the bottom rowand center column. X must play that position to prevent O from winning:O XX OXRepresenting the board in a computerIn order to write a program for tic-tac-toe, you need a way of representingthe game board using C++ variables. I recommend using one variable2for each of the 9 game cells. We can make things more convenient forourselves by representing cells as integers using the following convention:Cell Contents Integer Codeempty 0X 1O 2In the remainder of the handout, I will use the following notation to referto individual cells of the board:A B CD E FG H IUsing this convention, X moved to cell B in Example 1. X moved to cell Hin Example 2.You may use these letter codes to inspire your variable names, but this isnot required.LimitationYour computer player only needs to consider moves in the top row of the gameboard. In other words, your program should consider moving into cells A, B, and C, but does not need to worry about other cells.This limitation helps to keep the scope of the project within reason. Consideringmoves to all 9 cells, using what we’ve covered so far, would require a very long and repetitive program.After we cover arrays later in the semester, you might want to think abouthow you handle the full 3 × 3 board concisely using arrays.3What to doYour program should:1. Prompt the user to type in the state of the board, represented by 9integers between 0 and 2.2. Print a picture of the board to standard output.3. Decide on a move for the X player. Only cells A, B, and C need to beconsidered.4. Print out a description of X’s move, including the following pieces ofinformation:• the cell X moves to, as a letter A-I• if this move lets X win, say that• otherwise, if this move blocks O from winning, say that5. Print out a picture of the board, including X’s move.For full credit, your player must use the following strategy:1. If X has any winning move available, it must take it.2. Otherwise, if X can block O from winning, X must do so.3. Otherwise, X can pick any empty cell among A, B, or C.In other words, your computer player must first try to win if possible. If it cannot win, it must then try to block if possible. If winning and blocking are both impossible, it may pick any cell in the first row (A–C) that does not already have an X or O.If you have time and interest, you could extend the game in any of the following ways:1. Make your player take clever moves when there are no obvious winsor blocks. 42. Allow your player to move into cells D — I.3. Play a full game, from start to finish, instead of a single move. All of these are optional.5 Sample input/outputIn the following example, X takes a winning move:En te r board s t a t e as 9 i n t e g e r s , 0−2:1 1 0 2 0 0 0 2 0Curren t board :+−+−+−+| 1 | 1 | 0 |+−+−+−+| 2 | 0 | 0 |+−+−+−+| 0 | 2 | 0 |+−+−+−+X moves to C to win !New board :+−+−+−+| 1 | 1 | 1 |+−+−+−+| 2 | 0 | 0 |+−+−+−+| 0 | 2 | 0 |+−+−+−+6Next, X has no winning moves, and takes a blocking move:En te r board s t a t e as 9 i n t e g e r s , 0−2:1 0 0 0 2 0 0 2 1Curren t board :+−+−+−+| 1 | 0 | 0 |+−+−+−+| 0 | 2 | 0 |+−+−+−+| 0 | 2 | 1 |+−+−+−+X moves to B to block .New board :+−+−+−+| 1 | 1 | 0 |+−+−+−+| 0 | 2 | 0 |+−+−+−+| 0 | 2 | 1 |+−+−+−+7Finally, X has neither a winning nor blocking move, and may move toeither A or B; C is not empty. This computer player chooses B, although Awould’ve been a better choice.En te r board s t a t e as 9 i n t e g e r s , 0−2:0 0 2 0 0 0 1 0 0Curren t board :+−+−+−+| 0 | 0 | 2 |+−+−+−+| 0 | 0 | 0 |+−+−+−+| 1 | 0 | 0 |+−+−+−+X moves to BNew board :+−+−+−+| 0 | 1 | 2 |+−+−+−+| 0 | 0 | 0 |+−+−+−+| 1 | 0 | 0 |+−+−+−+What to turn inYour submission will be a written report, submitted electronically as a PDFfile through Moodle. Your report should include1. Your C++ source code.2. Your structure chart.3. A the input and output for each of the three examples above:(a) 1 1 0 2 0 0 0 2 0 (your program should choose C to win)(b) 1 0 0 0 2 0 0 2 1 (your program should choose B to block)(c) 0 0 2 0 0 0 1 0 0 (your program can choose A or B)4. The input and output for different examples where…(a) …X must choose a winning move(b) …X must choose a blocking move(c) …X has neither a winning nor blocking move8(d) …only one cell is empty, and X chooses itThere should be a total of 7 input/output dialogues.The first page must include your name and a title. If you work as a pair,list both partner’s names, and turn in only one submission.HintsLogicTo work properly, your program must first look for winning moves. Ifnone are found, your program must then look for blocking moves. If neitherof those kinds of moves are available, your program can look for openspace. It is important that these alternatives be considered in that order;this kind of situation implies that your program will need to consider possibilitiesin an if/else manner.I think the most straightforward way of approaching this is to write a verylong chain of if/else if/else if/else if/…/else statements.Each if or else if considers one potential move that X could make.There may, however, be other ways of approaching this.Printing out the board Conveniently, all three possible board states 0, 1, and 2, can be printed using exactly 1 character. So you can print a tidy grid using a single columnfor each cell. You can create the horizontal lines using the minus character ’-’, the vertical lines using the pipe character ’|’, and the intersections using the pluscharacter ’+’.The sample outputs are based on both these tips.9Printing out aspects of the move I have two suggestions about how to print out the description of X’s move. First, if you have an if statement that decides on a move, you could print out a message about that move between the curly braces (f and g) that define the statement’s body.A second approach is to have an char variable for the cell X chooses, and bool variables for whether the move is a win, and whether it’s a block.Then, after your program has decided where to move, you can have one set of print statements that use those variables.Getting startedThere are a lot of details to this project; too many to simply start programming without any forethought. Developing a structure chart before you start programming will be particularly important for this project.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] SOLVED:Intelligent Tic Tac Toe
30 $