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

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
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
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

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!