SlideShare a Scribd company logo
1 of 2
Description 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)
Description In a game of Tic Tac Toe- two players take turns making an.docx

More Related Content

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

#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
aquapariwar
 
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
 

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

#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
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
 
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)
 
python.pptx
python.pptxpython.pptx
python.pptx
 
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
 
Shoot-for-A-Star
Shoot-for-A-StarShoot-for-A-Star
Shoot-for-A-Star
 
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
 
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
 
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
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
 
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184
 

More from theresar3

Demographic Transition 1- Construct a graph of birth and death rates i.docx
Demographic Transition 1- Construct a graph of birth and death rates i.docxDemographic Transition 1- Construct a graph of birth and death rates i.docx
Demographic Transition 1- Construct a graph of birth and death rates i.docx
theresar3
 
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docx
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docxDefinitions- 1- Artery -Vessels transporting blood away from a heart c.docx
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docx
theresar3
 

More from theresar3 (20)

Designing the Future of Personal Fashion Kristen Vaccaro- Tanvi Agarw.docx
Designing the Future of Personal Fashion  Kristen Vaccaro- Tanvi Agarw.docxDesigning the Future of Personal Fashion  Kristen Vaccaro- Tanvi Agarw.docx
Designing the Future of Personal Fashion Kristen Vaccaro- Tanvi Agarw.docx
 
Design and create the Context Level Data Flow Diagram and ER Diagram o.docx
Design and create the Context Level Data Flow Diagram and ER Diagram o.docxDesign and create the Context Level Data Flow Diagram and ER Diagram o.docx
Design and create the Context Level Data Flow Diagram and ER Diagram o.docx
 
Design a database to keep data about college students- their academic.docx
Design a database to keep data about college students- their academic.docxDesign a database to keep data about college students- their academic.docx
Design a database to keep data about college students- their academic.docx
 
Desiendny Hxperimens in Test the Hypurtosia Representing the Data Yisu.docx
Desiendny Hxperimens in Test the Hypurtosia Representing the Data Yisu.docxDesiendny Hxperimens in Test the Hypurtosia Representing the Data Yisu.docx
Desiendny Hxperimens in Test the Hypurtosia Representing the Data Yisu.docx
 
Describe two (2) ways that a biomedical department with 3D printing ca.docx
Describe two (2) ways that a biomedical department with 3D printing ca.docxDescribe two (2) ways that a biomedical department with 3D printing ca.docx
Describe two (2) ways that a biomedical department with 3D printing ca.docx
 
describes a phospholipid bilayer in which embedded proteins can move l.docx
describes a phospholipid bilayer in which embedded proteins can move l.docxdescribes a phospholipid bilayer in which embedded proteins can move l.docx
describes a phospholipid bilayer in which embedded proteins can move l.docx
 
Description Create a file called -ingest_this-txt- type the following.docx
Description Create a file called -ingest_this-txt- type the following.docxDescription Create a file called -ingest_this-txt- type the following.docx
Description Create a file called -ingest_this-txt- type the following.docx
 
Descripe Rock- Hint- this is loaded with diatoms- What are diatoms- a.docx
Descripe Rock-  Hint- this is loaded with diatoms- What are diatoms- a.docxDescripe Rock-  Hint- this is loaded with diatoms- What are diatoms- a.docx
Descripe Rock- Hint- this is loaded with diatoms- What are diatoms- a.docx
 
Describe the advantages and disadvantages of using peer-to-peer networ.docx
Describe the advantages and disadvantages of using peer-to-peer networ.docxDescribe the advantages and disadvantages of using peer-to-peer networ.docx
Describe the advantages and disadvantages of using peer-to-peer networ.docx
 
Describe the chemical (including chemical bonds) and physical properti.docx
Describe the chemical (including chemical bonds) and physical properti.docxDescribe the chemical (including chemical bonds) and physical properti.docx
Describe the chemical (including chemical bonds) and physical properti.docx
 
Depreciation expense was $52-000 Use the corperase tax tates showa in.docx
Depreciation expense was $52-000 Use the corperase tax tates showa in.docxDepreciation expense was $52-000 Use the corperase tax tates showa in.docx
Depreciation expense was $52-000 Use the corperase tax tates showa in.docx
 
Demographic Transition 1- Construct a graph of birth and death rates i.docx
Demographic Transition 1- Construct a graph of birth and death rates i.docxDemographic Transition 1- Construct a graph of birth and death rates i.docx
Demographic Transition 1- Construct a graph of birth and death rates i.docx
 
Degree of combined leverage- Multiple Choice should be minimized by th.docx
Degree of combined leverage- Multiple Choice should be minimized by th.docxDegree of combined leverage- Multiple Choice should be minimized by th.docx
Degree of combined leverage- Multiple Choice should be minimized by th.docx
 
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docx
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docxDefinitions- 1- Artery -Vessels transporting blood away from a heart c.docx
Definitions- 1- Artery -Vessels transporting blood away from a heart c.docx
 
Define lexeme and token- For the code segment below- identify and list.docx
Define lexeme and token- For the code segment below- identify and list.docxDefine lexeme and token- For the code segment below- identify and list.docx
Define lexeme and token- For the code segment below- identify and list.docx
 
Define lifetime budget constraint representative consume in the two-pe.docx
Define lifetime budget constraint representative consume in the two-pe.docxDefine lifetime budget constraint representative consume in the two-pe.docx
Define lifetime budget constraint representative consume in the two-pe.docx
 
define and Explain Diversity and how organizations should manage it in.docx
define and Explain Diversity and how organizations should manage it in.docxdefine and Explain Diversity and how organizations should manage it in.docx
define and Explain Diversity and how organizations should manage it in.docx
 
Define and Explain each one with an example - Prejudice and Discrimina.docx
Define and Explain each one with an example - Prejudice and Discrimina.docxDefine and Explain each one with an example - Prejudice and Discrimina.docx
Define and Explain each one with an example - Prejudice and Discrimina.docx
 
Define a function named SwapValues that takes four integers as paramet.docx
Define a function named SwapValues that takes four integers as paramet.docxDefine a function named SwapValues that takes four integers as paramet.docx
Define a function named SwapValues that takes four integers as paramet.docx
 
Deferred tax liabilities can arise from a revenue being reported on th.docx
Deferred tax liabilities can arise from a revenue being reported on th.docxDeferred tax liabilities can arise from a revenue being reported on th.docx
Deferred tax liabilities can arise from a revenue being reported on th.docx
 

Recently uploaded

The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 
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)

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
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
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...
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
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
 
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
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdf
 
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
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
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
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 

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

  • 1. Description 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)