SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a 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
 

Semelhante a 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
 

Mais de MichaelQEBMartinc

Mais de 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
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

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)