Java Kernel for Jupyter Notebooks.
[Install Java kernel readme}(https://github.com/SpencerPark/IJava). Java will require an independent kernel in Jupyter Notebooks. The instruction performed by the Teacher follows, but look to readme if you have troubles.
(base) id:~$ wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip # download IJava kernel as zip
(base) id:~$ unzip ijava-1.3.0.zip # unzip downloaded IJava kernel
(base) id:~$ python install.py --user # install IJava kernel
(base) id:~$ jupyter kernelspec list # list kernels
Available kernels:
java /home/shay/.local/share/jupyter/kernels/java
python3 /home/shay/.local/share/jupyter/kernels/python3
Console Game Menu
College Boards Units #1, #3, and #4 and Free Response Methods and Control Structures are built into these labs. Of course, these games are very popular in beginning programming. They are here for reference, as they were shared by a student.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class ConsoleGame {
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
public ConsoleGame() {
Scanner sc = new Scanner(System.in); // using Java Scanner Object
boolean quit = false;
while (!quit) {
this.menuString(); // print Menu
try {
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
public void menuString(){
String menuText = ""
+ "\u001B[33m___________________________\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "|\u001B[0m Menu! \u001B[32m|\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "| 0 - Exit |\n"
+ "| 1 - Rock Paper Scissors |\n"
+ "| 2 - Higher or Lower |\n"
+ "| 3 - Tic Tac Toe |\n"
+ "|_________________________| \u001B[0m\n"
+ "\n"
+ "Choose an option.\n"
;
System.out.println(menuText);
}
private void chooseGame() {
this.menuString();
Scanner sc = new Scanner(System.in);
try {
int choice = sc.nextInt();
System.out.print("" + choice + ": ");
if (!this.action(choice)) {
chooseGame();
}
} catch (Exception e) {
sc.nextLine(); // Clear buffer
System.out.println(e + ": Not a number, try again.");
chooseGame(); // recursive loops, Unit 10 Topic
}
sc.close();
}
private boolean action(int selection) {
boolean quit = false;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
rps();
break;
case 2:
horl();
break;
case 3:
ticTacToe();
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
public void horl(){
System.out.println("Higher or Lower");
System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
System.out.println("If you guess the number correctly, you win!");
Scanner scHL = new Scanner(System.in);
int randomG = (int) (Math.random() * 8) + 1;
int guess = scHL.nextInt();
for(int i = 3; i > 0; i--){
if(guess == randomG){
System.out.println("You win!");
break;
}
else if(guess > randomG){
System.out.println("The number is lower");
}
else if(guess < randomG){
System.out.println("The number is higher");
}
guess = scHL.nextInt();
}
System.out.println("Game over.");
scHL.close();
}
public void rps(){
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false;
int random = (int) (Math.random() * 3);
while(quit == false){
if(userChoice.equals("r")){
if(random == 1){
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
}
else if(random == 2){
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
}
else{
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
}
else if(userChoice.equals("p")){
if(random == 1){
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
}
else if(random == 2){
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
}
else{
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
}
else if(userChoice.equals("s")){
if(random == 1){
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
}
else if(random == 2){
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
}
else{
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
}
else{
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
public void ticTacToe(){
System.out.println("Tic Tac Toe");
Scanner scTTT = new Scanner(System.in);
String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; // Array made here,
String player = "X";
String player2 = "O";
int turn = 0;
Boolean quit = false;
System.out.println("Do you want to play against a friend or the computer?");
System.out.println("Type 1 for friend, 2 for computer");
int choice = scTTT.nextInt();
//make tic tac toe using player1 and player2
if(choice == 1){
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Player 2's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = scTTT.nextInt();
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = player2;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
}
}
}
}
}
if(choice == 2){
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Computer's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = (int)(Math.random() * 9) + 1;
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = computer;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
}
}
}
}
}
scTTT.close();
}
static public void main(String[] args) {
new ConsoleGame(); // starting Menu object
}
}
ConsoleGame.main(null);
[33m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [32m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
1: Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock
The computer chose rock
It's a tie!
[0m
[33m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [32m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
0: Goodbye, World![0m
Higher or Lower Game + Explanation
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class HorlGame{
public void horlgame(){
System.out.println("Higher or Lower");
System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
System.out.println("If you guess the number correctly, you win!");
Scanner scHL = new Scanner(System.in);
int randomG = (int) (Math.random() * 8) + 1; // example of primitive data type int, Unit 1 Topic
int guess = scHL.nextInt();
for(int i = 3; i > 0; i--){
if(guess == randomG){
System.out.println("You win!");
break;
}
else if(guess > randomG){
System.out.println("The number is lower");
}
else if(guess < randomG){
System.out.println("The number is higher");
}
guess = scHL.nextInt();
}
System.out.println("Game over.");
scHL.close();
}
public static void main(String[] args){
HorlGame game = new HorlGame();
game.horlgame();
}
}
// Note, there is an issue with the code not outputting here, but it works perfectly fine on a Java Compiler online
HorlGame.main(null);
Higher or Lower
You have three guesses to guess the number I am thinking of between 1-8.
If you guess the number correctly, you win!
The number is higher
The number is higher
The number is lower
Game over.
Note: the word “procedure” and “function” will be used here interchangeably.
The import statements at the very top ensure that we are able to use certain commands that come from Java libraries, such as the scanner from java.util.Scanner and the random number command from the java.lang.Math library. Both of these import statements will be used for the Rock Paper Scissors and Tic Tac Toe games as well.
The “public void horlgame” acts as the function/procedure for this game, with void indicating that the function does not return anything at the end of it. There are also no parameters inside the function, which indicates that all the function is doing is running all of the code inside of it.
The variable randomG is a random number between 1 and 8 that the computer chooses, and that will be the number that the user has to guess in three guesses or less. There is also a variable (integer) called guess that is set to scHL.nextInt, which indicates that the value of variable guess is dependent on what user inputs into the scanner. The for loop indicates that all of the code within the loop will be ran until the user uses all three of their guesses and does not get it correct or until the user gets it correct in three guesses or less. If the user’s guess is equivalent to randomG, the program will print out the “You win!” message and will break (exit) the loop. If the user’s guess is greater than randomG, then the user will prompted with the “The number is lower” message. And of course, if the user’s guess is less than randomG, then the user will be prompted with the “The number is higher” message. If a user guesses wrong but still has at least one more guess remaining, the user will be allowed to guess again (guess = scHL.nextInt()). If the user uses up all three of their guesses but does not guess the random number correctly, they will be prompted with the “Game Over” message as well as what the actual number was.
Lastly, the public static void main is the main method that calls this function and allows the user to play the game. In the main method, we create a new object called game that is then used as an instance of the procedure horlgame so that all of the code in the procedure is executed.
Rock, Paper, Scissors Game + Explanation
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class RpsGame{
public void rps(){
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false; // Boolean expression example, Unit 3 Topic
int random = (int) (Math.random() * 3);
while(quit == false){ // If statement example, along with boolean expressions is also a Unit 3 Topic
if(userChoice.equals("r")){
if(random == 1){
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
}
else if(random == 2){
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
}
else{
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
}
else if(userChoice.equals("p")){
if(random == 1){
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
}
else if(random == 2){
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
}
else{
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
}
else if(userChoice.equals("s")){
if(random == 1){
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
}
else if(random == 2){
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
}
else{
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
}
else{
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
public static void main(String[] args){
RpsGame game1 = new RpsGame(); // creating a new object in Java, Unit 2 Topic
game1.rps();
}
}
// Note, there is an issue with the code not outputting here, but it works perfectly fine on a Java Compiler online
RpsGame.main(null);
Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock
The computer chose paper
You lose!
Note: the word “procedure” and “function” will be used here interchangeably.
The public class “RpsGame” allows us to create the many objects and instances shown above. But more importantly, the function “rps” can be thought of as the procedure pr function for this entire game. All of the code in this procedure will be executed upon calling it, which is clearly done in the main method at the bottom of the code cell.
In the rps function, several variables are defined, such as the scanner scRPS, which allows the user to input their choice of r for rock, p for paper, or s for scissors. The string userChoice is defined using the assignment made to the scanner, as that indicates that the variable is set to whichever letter (r, p , or s) that the user chooses. Furthermore, the boolean variable quit is set to false, as that indicates that the game is still going on upon the user executing the program. The variable quit is only set to true if the user loses against the computer.
Another important thing to note is that what the computer chooses is dependent on setting a variable called random equivalent to a random number between 1 and 3, with 1 corresponding to paper, 2 corresponding to scissors, and 3 corresponding to rock.
The rest of the code after that is pretty self explanatory, but essentially, if a user chooses something and the computer chooses something that can beat the user’s choice, the user will be provided with the “You Lost!” message (ex. user chooses scissors and computer chooses rock). If the user chooses something that can beat the computer’s choice, the user will be provided with the “You Won!” message (ex. user chooses scissors and computer chooses paper). Otherwise, if both the user and the computer choose the same thing, the game will end in a tie, and thus the message indicated that the game was a tie will be printed out as well.
And of course, if the user tries to input something other than the characters r, p , or s, they will be informed of their input being invalid and will be asked to try again, creating a new line for the user to enter their valid choice into. Whether the user wins or loses the game, the scanner scRPS will be closed at the end.
Lastly, the main method is what essentially calls this entire procedure. We create a new object called game1 so that it becomes an instance of the procedure rps described earlier, thus calling the function and allowing us to play the game.
Tic Tac Toe Game + Explanation
import java.util.Scanner;
import java.util.Random;
public class TicTacToe { // creating a class called TicTacToe, which sets us up for creating the actual game, Unit 5 Topic
private Scanner scanner = new Scanner(System.in);
private String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; // Array made here, Unit 6 Topic
private String player1 = "X";
private String player2 = "O";
private Random random = new Random();
private int turn = 0;
private boolean quit = false;
public void printBoard() {
for (int i = 0; i < 9; i += 3) { // Iterates through the board array and prints out each element of the board, Unit 4 Topic
System.out.println(board[i] + " | " + board[i + 1] + " | " + board[i + 2]);
}
}
public boolean makeMove(int move) {
if (move < 1 || move > 9 || !board[move - 1].equals(String.valueOf(move))) {
System.out.println("Invalid move. Try again.");
return false;
}
board[move - 1] = player1;
return true;
}
public boolean checkWin() {
int[][] winningCombos = { // 2D Array used here, Unit 8 Topic
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns
{0, 4, 8}, {2, 4, 6} // Diagonals
};
for (int[] combo : winningCombos) {
if (board[combo[0]].equals(player1) &&
board[combo[1]].equals(player1) &&
board[combo[2]].equals(player1)) {
return true;
}
}
return false;
}
public void playAgainstFriend() {
System.out.println("Type the number of the square you want to place your piece in");
while (!quit) {
System.out.println(player1 + "'s turn");
printBoard();
int move = scanner.nextInt();
if (makeMove(move)) {
turn++;
if (checkWin()) {
System.out.println(player1 + " wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = (player1.equals("X")) ? "O" : "X";
}
}
}
}
public void playAgainstComputer() {
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while (!quit) {
if (player1.equals("X")) {
System.out.println(player1 + "'s turn\n");
printBoard();
int move = scanner.nextInt();
if (makeMove(move)) {
turn++;
if (checkWin()) {
System.out.println(player1 + " wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = "O";
}
}
} else {
System.out.println("Computer's turn\n");
printBoard();
int move2 = random.nextInt(9) + 1;
if (makeMove(move2)) {
turn++;
if (checkWin()) {
System.out.println("Computer wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = "X";
}
}
}
}
}
public void play() {
System.out.println("Tic Tac Toe");
System.out.println("Do you want to play against a friend or the computer?\n");
System.out.println("Type 1 for friend, 2 for computer: ");
int choice = scanner.nextInt();
if (choice == 1) {
playAgainstFriend();
} else if (choice == 2) {
playAgainstComputer();
}
scanner.close();
}
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.play();
}
}
TicTacToe.main(null);
Tic Tac Toe
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer:
Type the number of the square you want to place your piece in
X's turn
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn
X | 2 | 3
4 | 5 | 6
7 | 8 | 9
X's turn
X | 2 | 3
4 | 5 | O
7 | 8 | 9
Computer's turn
X | X | 3
4 | 5 | O
7 | 8 | 9
Invalid move. Try again.
Computer's turn
X | X | 3
4 | 5 | O
7 | 8 | 9
X's turn
X | X | 3
4 | O | O
7 | 8 | 9
X wins!
Hangman Console Game in Java
import java.util.Random;
import java.util.Scanner;
public class HangmanGame {
public static void updateProgress(char letter, String word, char[] blanks, char[] progress) {
for (int i = 0; i < word.length(); i++) {
if (letter == word.charAt(i)) {
blanks[i * 2] = word.charAt(i);
progress[i] = word.charAt(i);
}
}
}
public static void main(String[] args) {
String[] wordList = {"apple", "money", "grades", "college"};
String word = wordList[new Random().nextInt(4)];
int lives = 7;
char[] blanks = new char[word.length() * 2];
for (int i = 0; i < blanks.length; i += 2) {
blanks[i] = '_';
blanks[i + 1] = ' ';
}
char[] progress = new char[word.length()];
Scanner scanner = new Scanner(System.in);
while (lives > 0) {
if (new String(progress).equals(word)) {
System.out.println("You Win!");
break;
}
System.out.println(blanks);
System.out.print("Enter a letter: ");
char userInput = scanner.next().charAt(0);
if (word.contains(String.valueOf(userInput))) {
System.out.println("The letter " + userInput + " is in the word.");
updateProgress(userInput, word, blanks, progress);
} else {
lives--;
System.out.println("Sorry! The letter " + userInput + " is not in the word.");
}
}
if (lives == 0) {
System.out.println("You Lost! The word was " + word + ".");
}
}
}
HangmanGame.main(null);
_ _ _ _ _ _
Enter a letter: The letter a is in the word.
_ _ a _ _ _
Enter a letter: The letter g is in the word.
g _ a _ _ _
Enter a letter: The letter r is in the word.
g r a _ _ _
Enter a letter: Sorry! The letter i is not in the word.
g r a _ _ _
Enter a letter: Sorry! The letter f is not in the word.
g r a _ _ _
Enter a letter: Sorry! The letter c is not in the word.
g r a _ _ _
Enter a letter: Sorry! The letter k is not in the word.
g r a _ _ _
Enter a letter: The letter s is in the word.
g r a _ _ s
Enter a letter: Sorry! The letter q is not in the word.
g r a _ _ s
Enter a letter: Sorry! The letter p is not in the word.
g r a _ _ s
Enter a letter: Sorry! The letter l is not in the word.
You Lost! The word was grades.
Note: the word “procedure” and “function” will be used here interchangeably.
The code above defines a class called TicTacToe that allows players to play against each other or against a computer. The game is played on a 3x3 board. The class contains methods to print the game board, make moves, check for wins, and control the game flow. The game can be played in two modes: against a friend or against the computer.
In the friend mode, players take turns entering their moves by typing the number of the square where they want to place their piece. The game tracks the progress of the game, checks for a win or tie condition, and switches turns between the two players. If a win or tie occurs, the game ends.
In the computer mode, the player can play against an automated computer opponent. The player plays as “X,” and the computer plays as “O.” The computer generates its moves randomly. The game loop continues until there is a win or a tie, with the same end conditions as in the friend mode.
The main method of the class creates an instance of TicTacToe and starts the game by calling the play method. Players choose whether to play against a friend or the computer by inputting their choice. After the game is over, the scanner is closed. Overall, the code provides a functional representation of the Tic-Tac-Toe game, allowing for interactive play between players and the computer.
Answers to Questions About Reorganization and AP Identification
I think that reorganization is important because by simplifying and reorganizing the code, we can get a better idea of what each individual game looks like when they are ran in their own cells rather than being chosen from a menu as shown in the first code cell. Also, simplifying the code makes it much easier to read, allowing us to follow along with what the logic is doing much better. Simplifying the code can also make it easier for us to identify any errors, as if we write an unnecessarily enormous amount of code and run into any problems, it can be much more difficult to find where the problem is as we have to scroll through many lines of code, most of which are duplicated. On the topic of duplicate code, simplifying and reorganizing the code can reduce the redundancy of the code, as if we just repeat the same lines of code multiple times when it could be replaced with something much simpler, it is a poor example of programming in Java.
I think that AP Identification is important because it will play a big role in getting us ready for the AP exam in May. If we can identify the different units of the class that a program uses, we will be able to understand the code much better and thus have a greater ability to read other people’s code, which is what the multiple choice section of the exam is mainly comprised of. Also, understanding what is an example of a primitive type or recursion will help us in implementing the units for the class in our own code, which will be key to doing well on the FRQ portion of the exam (4 Qs).
All in all, reorganization and AP Identification are both useful methods to help us improve our own code in general and improve our ability to read other peoples’ code, both of which will be essential to doing well in this class and on the AP Exam.
Hacks
To start the year, I want you to consider a simple Java console game or improve on the organization and presentation of the games listed.
- Make RPS, Tic-Tack-Toe, and Higher Lower into different cells and objects. Document each cell in Jupyter Notebooks.
- Simplify logic, particularly T-T-T. What could you do to make this more simple? Java has HashMap (like Python Dictionary), Arrays (fixed size), ArraLists (Dynamic Size).
- Run the menu using recursion versus while loop. Try to color differently.
- Look over 10 units for College Board AP Computer Science A. In your reorganized code blocks and comments identify the Units of Code Used.
- Answer why you think this reorganization and AP indetification is important?