SlideShare a Scribd company logo
1 of 7
1. In NetBeans, create a new Java Application project named
TicTacToeGame.
2. Add the following variable and constant declarations to the
TicTacToeGame class:
static int[][] gameboard; static final int EMPTY = 0; static final
int NOUGHT = -1; static final int CROSS = 1;
Note: The variable gameboard is a two-dimensional int array.
Think of it as a table with rows and columns, where a cell can
be empty (0) or contain a nought (–1) or cross (1) . The
constants EMPTY, NOUGHT, and CROSS will simplify your
code.
3. Add the following utility methods to the TicTacToeGame
class:
static void set(int val, int row, int col) throws
IllegalArgumentException {
if (gameboard[row][col] == EMPTY) gameboard[row][col] =
val;
else throw new IllegalArgumentException(“Player already
there!”);
}
static void displayBoard() {
for( int r = 0; r < gameboard.length; r++ ) {
System.out.print(“|”);
for (int c = 0; c < gameboard[r].length; c++) {
switch(gameboard[r][c]) {
case NOUGHT: System.out.print(“O”); break;
case CROSS: System.out.print(“X”); break;
default: //Empty System.out.print(“ “);
72
Graded Project
}
System.out.print(“|”);
}
System.out.println(“n———-n”);
}
}
4. Add the following method signatures to the
TicTacToeGame class:
5. Define the createBoard method.
6. Define the winOrTie method. Check first for a win with rows
and columns and then diagonally. Finally, check to see if there
are any empty cells without a cross or naught.
static void createBoard(int rows, int cols) {
//Initialize the gameboard
}
static boolean winOrTie() {
//Determine whether X or 0 won or there is a tie
}
Note: Review the sections “Initializing Multidimensional
Arrays” on pages 144–145 and “Iterating Over
Multidimensional Arrays” on pages 156–158 for how to
initialize and iterate through a multidimensional array. A player
wins if all the cells in a row or column are the same mark or
diagonally through the center. The players tie if all cells have a
cross or nought, but no player has three marks horizontally,
verti- cally, or diagonally. Return NOUGHT if nought wins,
CROSS if cross wins, 0 if there’s a tie, and another value (like
–2, for example) if there are empty cells on the board.
7. In the main() method, perform the following actions:
a. Create the board and initialize a turn counter, player value,
and game outcome. For nought, the value is
–1, while 1 is the value for cross.
b. While there’s no winner or tie, display the board and prompt
for a row and column for the current player.
73
Graded Project
c. Use a try/catch block to handle the exception from the set
method. You can use the System.err.println method rather than
the System.out.println method to output the exception. This will
display the message in red.
d. Display the final board and a message on which player won
or if there’s a tie.
8. When completed, the contents of the main() method should
resemble the following:
createBoard(3,3); int turn = 0;
int playerVal; int outcome;
java.util.Scanner scan = new java.util.Scanner(System.in); do {
displayBoard();
playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
if (playerVal == NOUGHT) System.out.println(“n—O’s turn—
”); else System.out.println(“n—X’s turn—”);
System.out.print(“Enter row and column:”);
try {
set(playerVal, scan.nextInt(), scan.nextInt());
} catch (Exception ex) {System.err.println(ex);} turn ++;
outcome = winOrTie();
} while ( outcome == -2 ); displayBoard();
switch (outcome) { case NOUGHT:
System.out.println(“O wins!”); break;
case CROSS: System.out.println(“X wins!”); break;
case 0: System.out.println(“Tie.”); break;
}
74
Graded Project
9. Compile and run the project to ensure it works as expected.
Try a few games to verify all wins and ties are correctly
detected.
The application should behave as follows in the Output window:
| | | |
| | | |
| | | |
—O’s turn—
Enter row and column:0 0
|O| | |
| | | |
| | | |
—X’s turn—
Enter row and column:0 1
|O|X| |
| | | |
| | | |
—O’s turn—
Enter row and column:1 1
|O|X| |
| |O| |
| | | |
—X’s turn—
Enter row and column:2 0
|O|X| |
| |O| |
|X| | |
75
Graded Project
—O’s turn—
Enter row and column: 2 2.
|O|X| |
| |O| |
|X| |O|
O wins!
1. In NetBeans, create a new Java Application project named TicTac.docx

More Related Content

Similar to 1. In NetBeans, create a new Java Application project named TicTac.docx

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
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
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
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 

Similar to 1. In NetBeans, create a new Java Application project named TicTac.docx (7)

10. Recursion
10. Recursion10. Recursion
10. Recursion
 
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
 
Android application - Tic Tac Toe
Android application - Tic Tac ToeAndroid application - Tic Tac Toe
Android application - Tic Tac Toe
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.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
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
Review questions and answers
Review questions and answersReview questions and answers
Review questions and answers
 

More from jackiewalcutt

Briefly summarize and analyze two primary sources, identifying their.docx
Briefly summarize and analyze two primary sources, identifying their.docxBriefly summarize and analyze two primary sources, identifying their.docx
Briefly summarize and analyze two primary sources, identifying their.docx
jackiewalcutt
 
Briefly describe how to deploy a Continuous Improvement effort.W.docx
Briefly describe how to deploy a Continuous Improvement effort.W.docxBriefly describe how to deploy a Continuous Improvement effort.W.docx
Briefly describe how to deploy a Continuous Improvement effort.W.docx
jackiewalcutt
 

More from jackiewalcutt (20)

briefly summarize how the Electoral College works. Explain some of t.docx
briefly summarize how the Electoral College works. Explain some of t.docxbriefly summarize how the Electoral College works. Explain some of t.docx
briefly summarize how the Electoral College works. Explain some of t.docx
 
Briefly summarize and analyze two primary sources, identifying their.docx
Briefly summarize and analyze two primary sources, identifying their.docxBriefly summarize and analyze two primary sources, identifying their.docx
Briefly summarize and analyze two primary sources, identifying their.docx
 
Briefly respond to the following questions. Use facts and examples t.docx
Briefly respond to the following questions. Use facts and examples t.docxBriefly respond to the following questions. Use facts and examples t.docx
Briefly respond to the following questions. Use facts and examples t.docx
 
Briefly in your own words describe the distinction between explicit .docx
Briefly in your own words describe the distinction between explicit .docxBriefly in your own words describe the distinction between explicit .docx
Briefly in your own words describe the distinction between explicit .docx
 
Briefly explain   Victoria Australia Covid19 update and impact.docx
Briefly explain   Victoria Australia Covid19 update and impact.docxBriefly explain   Victoria Australia Covid19 update and impact.docx
Briefly explain   Victoria Australia Covid19 update and impact.docx
 
Briefly introduce the détente policies of the early 1970s, and des.docx
Briefly introduce the détente policies of the early 1970s, and des.docxBriefly introduce the détente policies of the early 1970s, and des.docx
Briefly introduce the détente policies of the early 1970s, and des.docx
 
Briefly explain the role of information systems in an organization.docx
Briefly explain the role of information systems in an organization.docxBriefly explain the role of information systems in an organization.docx
Briefly explain the role of information systems in an organization.docx
 
briefly describe, in 2-3 pages, the problemissue and the proble.docx
briefly describe, in 2-3 pages, the problemissue and the proble.docxbriefly describe, in 2-3 pages, the problemissue and the proble.docx
briefly describe, in 2-3 pages, the problemissue and the proble.docx
 
Briefly explain the mission of the OSH Act. What is the rationale be.docx
Briefly explain the mission of the OSH Act. What is the rationale be.docxBriefly explain the mission of the OSH Act. What is the rationale be.docx
Briefly explain the mission of the OSH Act. What is the rationale be.docx
 
Briefly discuss the various organizational approaches to managing .docx
Briefly discuss the various organizational approaches to managing .docxBriefly discuss the various organizational approaches to managing .docx
Briefly discuss the various organizational approaches to managing .docx
 
Briefly explain the identified security issues during Risk Assessmen.docx
Briefly explain the identified security issues during Risk Assessmen.docxBriefly explain the identified security issues during Risk Assessmen.docx
Briefly explain the identified security issues during Risk Assessmen.docx
 
Briefly discuss some KSAs for Fighting Cybercrime and submit in a wo.docx
Briefly discuss some KSAs for Fighting Cybercrime and submit in a wo.docxBriefly discuss some KSAs for Fighting Cybercrime and submit in a wo.docx
Briefly discuss some KSAs for Fighting Cybercrime and submit in a wo.docx
 
Briefly describe what a monopoly is and give an example using the ch.docx
Briefly describe what a monopoly is and give an example using the ch.docxBriefly describe what a monopoly is and give an example using the ch.docx
Briefly describe what a monopoly is and give an example using the ch.docx
 
Briefly describe the spread of industry throughout Europe and into.docx
Briefly describe the spread of industry throughout Europe and into.docxBriefly describe the spread of industry throughout Europe and into.docx
Briefly describe the spread of industry throughout Europe and into.docx
 
Briefly describe the path of food through the digestive system and e.docx
Briefly describe the path of food through the digestive system and e.docxBriefly describe the path of food through the digestive system and e.docx
Briefly describe the path of food through the digestive system and e.docx
 
Briefly describe the different parenting styles discussed in this we.docx
Briefly describe the different parenting styles discussed in this we.docxBriefly describe the different parenting styles discussed in this we.docx
Briefly describe the different parenting styles discussed in this we.docx
 
Briefly describe how the BIOS boots or starts the computer and.docx
Briefly describe how the BIOS boots or starts the computer and.docxBriefly describe how the BIOS boots or starts the computer and.docx
Briefly describe how the BIOS boots or starts the computer and.docx
 
Briefly describe how to deploy a Continuous Improvement effort.W.docx
Briefly describe how to deploy a Continuous Improvement effort.W.docxBriefly describe how to deploy a Continuous Improvement effort.W.docx
Briefly describe how to deploy a Continuous Improvement effort.W.docx
 
briefly define democracy and evaluate in detail THREE of.docx
briefly define democracy and evaluate in detail THREE of.docxbriefly define democracy and evaluate in detail THREE of.docx
briefly define democracy and evaluate in detail THREE of.docx
 
Briefly define, listcontrast, identify the significance of, or .docx
Briefly define, listcontrast, identify the significance of, or .docxBriefly define, listcontrast, identify the significance of, or .docx
Briefly define, listcontrast, identify the significance of, or .docx
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
Alexander Litvinenko
 

Recently uploaded (20)

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Benefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxBenefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptx
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
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Ư...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
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
 

1. In NetBeans, create a new Java Application project named TicTac.docx

  • 1. 1. In NetBeans, create a new Java Application project named TicTacToeGame. 2. Add the following variable and constant declarations to the TicTacToeGame class: static int[][] gameboard; static final int EMPTY = 0; static final int NOUGHT = -1; static final int CROSS = 1; Note: The variable gameboard is a two-dimensional int array. Think of it as a table with rows and columns, where a cell can be empty (0) or contain a nought (–1) or cross (1) . The constants EMPTY, NOUGHT, and CROSS will simplify your code. 3. Add the following utility methods to the TicTacToeGame class: static void set(int val, int row, int col) throws IllegalArgumentException { if (gameboard[row][col] == EMPTY) gameboard[row][col] = val; else throw new IllegalArgumentException(“Player already there!”); } static void displayBoard() { for( int r = 0; r < gameboard.length; r++ ) { System.out.print(“|”); for (int c = 0; c < gameboard[r].length; c++) { switch(gameboard[r][c]) { case NOUGHT: System.out.print(“O”); break; case CROSS: System.out.print(“X”); break; default: //Empty System.out.print(“ “);
  • 2. 72 Graded Project } System.out.print(“|”); } System.out.println(“n———-n”); } } 4. Add the following method signatures to the TicTacToeGame class: 5. Define the createBoard method. 6. Define the winOrTie method. Check first for a win with rows and columns and then diagonally. Finally, check to see if there are any empty cells without a cross or naught. static void createBoard(int rows, int cols) { //Initialize the gameboard } static boolean winOrTie() { //Determine whether X or 0 won or there is a tie } Note: Review the sections “Initializing Multidimensional Arrays” on pages 144–145 and “Iterating Over Multidimensional Arrays” on pages 156–158 for how to initialize and iterate through a multidimensional array. A player wins if all the cells in a row or column are the same mark or diagonally through the center. The players tie if all cells have a cross or nought, but no player has three marks horizontally,
  • 3. verti- cally, or diagonally. Return NOUGHT if nought wins, CROSS if cross wins, 0 if there’s a tie, and another value (like –2, for example) if there are empty cells on the board. 7. In the main() method, perform the following actions: a. Create the board and initialize a turn counter, player value, and game outcome. For nought, the value is –1, while 1 is the value for cross. b. While there’s no winner or tie, display the board and prompt for a row and column for the current player. 73 Graded Project c. Use a try/catch block to handle the exception from the set method. You can use the System.err.println method rather than the System.out.println method to output the exception. This will display the message in red. d. Display the final board and a message on which player won or if there’s a tie. 8. When completed, the contents of the main() method should resemble the following: createBoard(3,3); int turn = 0; int playerVal; int outcome; java.util.Scanner scan = new java.util.Scanner(System.in); do { displayBoard(); playerVal = (turn % 2 == 0)? NOUGHT : CROSS; if (playerVal == NOUGHT) System.out.println(“n—O’s turn— ”); else System.out.println(“n—X’s turn—”); System.out.print(“Enter row and column:”);
  • 4. try { set(playerVal, scan.nextInt(), scan.nextInt()); } catch (Exception ex) {System.err.println(ex);} turn ++; outcome = winOrTie(); } while ( outcome == -2 ); displayBoard(); switch (outcome) { case NOUGHT: System.out.println(“O wins!”); break; case CROSS: System.out.println(“X wins!”); break; case 0: System.out.println(“Tie.”); break; } 74 Graded Project 9. Compile and run the project to ensure it works as expected. Try a few games to verify all wins and ties are correctly detected. The application should behave as follows in the Output window: | | | | | | | | | | | | —O’s turn— Enter row and column:0 0 |O| | | | | | |
  • 5. | | | | —X’s turn— Enter row and column:0 1 |O|X| | | | | | | | | | —O’s turn— Enter row and column:1 1 |O|X| | | |O| | | | | | —X’s turn— Enter row and column:2 0 |O|X| | | |O| |
  • 6. |X| | | 75 Graded Project —O’s turn— Enter row and column: 2 2. |O|X| | | |O| | |X| |O| O wins!