SlideShare a Scribd company logo
1 of 2
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with
their respective tokens (either X or O). When one player has placed three tokens in a horizontal,
vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate
occurs when all the cells on the grid have been filled with tokens and neither player has achieved
a win. Write a program that emulates a Tic Tac Toe game. When you are done, a typical session
will look like this: Welcome to tic-tac-toe. Enter coordinates for your move following the X and
O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| ---
-- B | |O ----- C | | And so on. Illegal moves will prompt the user again for a new move. A win or
a stalemate will be announced, mentioning the winning side if any. The program will terminate
whenever a single game is complete. For this lab, you will be provided with a base file to work
with. The base file can be downloaded from: https://github.com/victoryu/CIS35A-LabsLinks to
an external site. You will add and/or modify the code, as instructed below. Do not change the
overall structure of the program. Just fill in with your code at TODO and Step #. This file has the
general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe
"board". The board will be represented internally by a two dimensional array of characters (3 x
3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance
variables as follows. private char[][] board; private char player; // 'X' or 'O' You will need to
define the following methods: 1. A constructor: public TicTacToe() to create an empty board,
with initial value of a space (' ') 2. play method public boolean play(String position) if position
represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return
true. Otherwise, return false. 3. switchTurn method public void switchTurn() switches the current
player from X to O, or vice versa. 4. won method public boolean won() Returns true if the
current player has filled three in a row, column or either diagonal. Otherwise, return false. 5.
stalemate method public boolean stalemate() Returns true if there are no places left to move; 6.
printBoard method public void print() prints the current board 7. Use the following test code in
your main method to create a TicTacToe object and print it using the printBoard method given,
so as to test your code. Your printBoard method should produce the first board in the example
above. public static void main(String[] args) { Scanner in = new Scanner(System.in); TicTacToe
game = new TicTacToe(); System.out.println("Welcome to Tic-tac-toe");
System.out.println("Enter coordinates for your move following the X and O prompts");
while(!game.stalemate()) { game.print(); System.out.print(game.getPlayer() + ":"); //Loop while
the method play does not return true when given their move. //Body of loop should ask for a
different move while(!game.play(in.next())) { System.out.println("Illegal move. Enter your
move."); System.out.print(game.getPlayer() + ":"); } //If the game is won, call break;
if(game.won()) break; //Switch the turn game.switchTurn(); } game.print(); if(game.won()) {
System.out.println("Player "+game.getPlayer()+" Wins!!!!"); } else {
System.out.println("Stalemate"); } } Test the following cases: 1. A player makes a legal
movement; 2. A player makes an illegal movement; 3. Player X wins 4. Player X loses 5.
Stalemate (neither player wins)
In a game of Tic Tac Toe- two players take turns making an available c.docx

More Related Content

Similar to In a game of Tic Tac Toe- two players take turns making an available c.docx

Connect4.c2. Include the string.h header file3. Declare the foll.pdf
Connect4.c2. Include the string.h header file3. Declare the foll.pdfConnect4.c2. Include the string.h header file3. Declare the foll.pdf
Connect4.c2. Include the string.h header file3. Declare the foll.pdf
shakilaghani
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
FashionColZone
 
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdfExercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
fms12345
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
This is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxThis is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docx
abhi353063
 
Objectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docxObjectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docx
amit657720
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
info382133
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
info430661
 

Similar to In a game of Tic Tac Toe- two players take turns making an available c.docx (14)

Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Connect4.c2. Include the string.h header file3. Declare the foll.pdf
Connect4.c2. Include the string.h header file3. Declare the foll.pdfConnect4.c2. Include the string.h header file3. Declare the foll.pdf
Connect4.c2. Include the string.h header file3. Declare the foll.pdf
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
 
Tic tac toe on c++ project
Tic tac toe on c++ projectTic tac toe on c++ project
Tic tac toe on c++ project
 
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdfExercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
python.pptx
python.pptxpython.pptx
python.pptx
 
Shoot-for-A-Star
Shoot-for-A-StarShoot-for-A-Star
Shoot-for-A-Star
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
 
This is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxThis is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docx
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88
 
Objectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docxObjectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docx
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
 

More from MichaelQEBMartinc

More from MichaelQEBMartinc (20)

int FUNCTION(unsigned int process_id) -{ task_t processes-MAX_PROCESSE.docx
int FUNCTION(unsigned int process_id) -{ task_t processes-MAX_PROCESSE.docxint FUNCTION(unsigned int process_id) -{ task_t processes-MAX_PROCESSE.docx
int FUNCTION(unsigned int process_id) -{ task_t processes-MAX_PROCESSE.docx
 
Instructions- Indicate whether each of the following statements is tru (1).docx
Instructions- Indicate whether each of the following statements is tru (1).docxInstructions- Indicate whether each of the following statements is tru (1).docx
Instructions- Indicate whether each of the following statements is tru (1).docx
 
Instructions- Please answer all questions in FULL sentences- Use APA.docx
Instructions-  Please answer all questions in FULL sentences-  Use APA.docxInstructions-  Please answer all questions in FULL sentences-  Use APA.docx
Instructions- Please answer all questions in FULL sentences- Use APA.docx
 
Instructions Part 1- Compare and contrast the similarities and differe.docx
Instructions Part 1- Compare and contrast the similarities and differe.docxInstructions Part 1- Compare and contrast the similarities and differe.docx
Instructions Part 1- Compare and contrast the similarities and differe.docx
 
Instructions Assign ICD-10-CM codes after interpreting coding conventi.docx
Instructions Assign ICD-10-CM codes after interpreting coding conventi.docxInstructions Assign ICD-10-CM codes after interpreting coding conventi.docx
Instructions Assign ICD-10-CM codes after interpreting coding conventi.docx
 
Instructions Amount Hershey needs- $1 billion to build four new manufa.docx
Instructions Amount Hershey needs- $1 billion to build four new manufa.docxInstructions Amount Hershey needs- $1 billion to build four new manufa.docx
Instructions Amount Hershey needs- $1 billion to build four new manufa.docx
 
Insertion Sort- 7- Copy paste the card images to the empty cells below.docx
Insertion Sort- 7- Copy paste the card images to the empty cells below.docxInsertion Sort- 7- Copy paste the card images to the empty cells below.docx
Insertion Sort- 7- Copy paste the card images to the empty cells below.docx
 
Input of preparatory reaction (1-2) Output of preparatory reaction (2-.docx
Input of preparatory reaction (1-2) Output of preparatory reaction (2-.docxInput of preparatory reaction (1-2) Output of preparatory reaction (2-.docx
Input of preparatory reaction (1-2) Output of preparatory reaction (2-.docx
 
Innovative Consulting Co- has the foliowhg accounts in its jodper Cash.docx
Innovative Consulting Co- has the foliowhg accounts in its jodper Cash.docxInnovative Consulting Co- has the foliowhg accounts in its jodper Cash.docx
Innovative Consulting Co- has the foliowhg accounts in its jodper Cash.docx
 
Information Technology Project Management Which of these types of p.docx
Information Technology Project Management    Which of these types of p.docxInformation Technology Project Management    Which of these types of p.docx
Information Technology Project Management Which of these types of p.docx
 
Indicate whether the following items can be classified on a statement.docx
Indicate whether the following items can be classified on a statement.docxIndicate whether the following items can be classified on a statement.docx
Indicate whether the following items can be classified on a statement.docx
 
Indigo Inc- uses a calendar year for financial reporting- The company (1).docx
Indigo Inc- uses a calendar year for financial reporting- The company (1).docxIndigo Inc- uses a calendar year for financial reporting- The company (1).docx
Indigo Inc- uses a calendar year for financial reporting- The company (1).docx
 
Income statement information for Martincz Tire Repair Corporation for.docx
Income statement information for Martincz Tire Repair Corporation for.docxIncome statement information for Martincz Tire Repair Corporation for.docx
Income statement information for Martincz Tire Repair Corporation for.docx
 
In your initial post- present an area or a country that currently has.docx
In your initial post- present an area or a country that currently has.docxIn your initial post- present an area or a country that currently has.docx
In your initial post- present an area or a country that currently has.docx
 
include calculations Problem 4- Consider the system x1-x13x2x2-x1x2 Is.docx
include calculations Problem 4- Consider the system x1-x13x2x2-x1x2 Is.docxinclude calculations Problem 4- Consider the system x1-x13x2x2-x1x2 Is.docx
include calculations Problem 4- Consider the system x1-x13x2x2-x1x2 Is.docx
 
In what areas will oxygen move into the blood- In the lungs None of th.docx
In what areas will oxygen move into the blood- In the lungs None of th.docxIn what areas will oxygen move into the blood- In the lungs None of th.docx
In what areas will oxygen move into the blood- In the lungs None of th.docx
 
In the Stevens Case- the Supreme Court determined that the defendant-.docx
In the Stevens Case- the Supreme Court determined that the defendant-.docxIn the Stevens Case- the Supreme Court determined that the defendant-.docx
In the Stevens Case- the Supreme Court determined that the defendant-.docx
 
In the last few years- many healthcare organizations have had challeng.docx
In the last few years- many healthcare organizations have had challeng.docxIn the last few years- many healthcare organizations have had challeng.docx
In the last few years- many healthcare organizations have had challeng.docx
 
In the plot below- the y-axis represented fractional saturation (Y) an.docx
In the plot below- the y-axis represented fractional saturation (Y) an.docxIn the plot below- the y-axis represented fractional saturation (Y) an.docx
In the plot below- the y-axis represented fractional saturation (Y) an.docx
 
In the histology image shown below- letter A represents the - Select-.docx
In the histology image shown below- letter A represents the - Select-.docxIn the histology image shown below- letter A represents the - Select-.docx
In the histology image shown below- letter A represents the - Select-.docx
 

Recently uploaded

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
factors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptxfactors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptx
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 

In a game of Tic Tac Toe- two players take turns making an available c.docx

  • 1. In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program that emulates a Tic Tac Toe game. When you are done, a typical session will look like this: Welcome to tic-tac-toe. Enter coordinates for your move following the X and O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| --- -- B | |O ----- C | | And so on. Illegal moves will prompt the user again for a new move. A win or a stalemate will be announced, mentioning the winning side if any. The program will terminate whenever a single game is complete. For this lab, you will be provided with a base file to work with. The base file can be downloaded from: https://github.com/victoryu/CIS35A-LabsLinks to an external site. You will add and/or modify the code, as instructed below. Do not change the overall structure of the program. Just fill in with your code at TODO and Step #. This file has the general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe "board". The board will be represented internally by a two dimensional array of characters (3 x 3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance variables as follows. private char[][] board; private char player; // 'X' or 'O' You will need to define the following methods: 1. A constructor: public TicTacToe() to create an empty board, with initial value of a space (' ') 2. play method public boolean play(String position) if position represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return true. Otherwise, return false. 3. switchTurn method public void switchTurn() switches the current player from X to O, or vice versa. 4. won method public boolean won() Returns true if the current player has filled three in a row, column or either diagonal. Otherwise, return false. 5. stalemate method public boolean stalemate() Returns true if there are no places left to move; 6. printBoard method public void print() prints the current board 7. Use the following test code in your main method to create a TicTacToe object and print it using the printBoard method given, so as to test your code. Your printBoard method should produce the first board in the example above. public static void main(String[] args) { Scanner in = new Scanner(System.in); TicTacToe game = new TicTacToe(); System.out.println("Welcome to Tic-tac-toe"); System.out.println("Enter coordinates for your move following the X and O prompts"); while(!game.stalemate()) { game.print(); System.out.print(game.getPlayer() + ":"); //Loop while the method play does not return true when given their move. //Body of loop should ask for a different move while(!game.play(in.next())) { System.out.println("Illegal move. Enter your move."); System.out.print(game.getPlayer() + ":"); } //If the game is won, call break; if(game.won()) break; //Switch the turn game.switchTurn(); } game.print(); if(game.won()) { System.out.println("Player "+game.getPlayer()+" Wins!!!!"); } else { System.out.println("Stalemate"); } } Test the following cases: 1. A player makes a legal movement; 2. A player makes an illegal movement; 3. Player X wins 4. Player X loses 5. Stalemate (neither player wins)