Removed spaces from top-level directory names.

Spaces tend to cause annoyances in a Unix-style shell environment.
This change fixes that.
This commit is contained in:
Chris Reuter
2021-11-21 18:30:21 -05:00
parent df2e7426eb
commit d26dbf036a
1725 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
Conversion to [Oracle Java](https://openjdk.java.net/)

View File

@@ -0,0 +1,96 @@
/**
* @author Ollie Hensman-Crook
*/
public class Board {
private char arr[];
public Board() {
this.arr = new char[9];
for (int x = 1; x <= 9; x++) {
this.arr[x - 1] = ' ';
}
}
/**
* Place 'X' or 'O' on the board position passed
* @param position
* @param player
*/
public void setArr(int position, char player) {
if (player == 'X') {
this.arr[position-1] = 'X';
} else {
this.arr[position -1] = 'O';
}
}
public void printBoard() {
System.out.format("%-3c ! %-3c ! %-3c\n----+----+----\n%-3c ! %-3c ! %-3c\n----+----+----\n%-3c ! %-3c ! %-3c\n",
this.arr[0], this.arr[1], this.arr[2], this.arr[3], this.arr[4], this.arr[5], this.arr[6], this.arr[7], this.arr[8]
);
}
/**
* @param x
* @return the value of the char at a given position
*/
public char getBoardValue(int x) {
return arr[x-1];
}
/**
* Go through the board and check for win (horizontal, diagonal, vertical)
* @param player
* @return whether a win has occured
*/
public boolean checkWin(char player) {
if(this.arr[0] == player && this.arr[1] == player && this.arr[2] == player)
return true;
if(this.arr[3] == player && this.arr[4] == player && this.arr[5] == player)
return true;
if(this.arr[6] == player && this.arr[7] == player && this.arr[8] == player)
return true;
if(this.arr[0] == player && this.arr[4] == player && this.arr[8] == player)
return true;
if(this.arr[2] == player && this.arr[4] == player && this.arr[6] == player)
return true;
if(this.arr[0] == player && this.arr[3] == player && this.arr[6] == player)
return true;
if(this.arr[1] == player && this.arr[4] == player && this.arr[7] == player)
return true;
if(this.arr[2] == player && this.arr[5] == player && this.arr[8] == player)
return true;
return false;
}
public boolean checkDraw() {
if(this.checkWin('X') == false && this.checkWin('O') == false) {
if(this.getBoardValue(1) != ' ' && this.getBoardValue(2) != ' ' && this.getBoardValue(3) != ' ' && this.getBoardValue(4) != ' ' && this.getBoardValue(5) != ' ' && this.getBoardValue(6) != ' ' && this.getBoardValue(7) != ' ' && this.getBoardValue(8) != ' ' && this.getBoardValue(9) != ' ' ) {
return true;
}
}
return false;
}
/**
* Reset the board
*/
public void clear() {
for (int x = 1; x <= 9; x++) {
this.arr[x - 1] = ' ';
}
}
}

View File

@@ -0,0 +1,157 @@
import java.util.Scanner;
import java.util.Random;
/**
* @author Ollie Hensman-Crook
*/
public class TicTacToe2 {
public static void main(String[] args) {
Board gameBoard = new Board();
Random compChoice = new Random();
char yourChar;
char compChar;
Scanner in = new Scanner(System.in);
System.out.println(" TIC-TAC-TOE");
System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
System.out.println("\nTHE BOARD IS NUMBERED: ");
System.out.println(" 1 2 3\n 4 5 6\n 7 8 9\n");
while (true) {
// ask if the player wants to be X or O and if their input is valid set their
// play piece as such
System.out.println("DO YOU WANT 'X' OR 'O'");
while (true) {
try {
char input;
input = in.next().charAt(0);
if (input == 'X' || input == 'x') {
yourChar = 'X';
compChar = 'O';
break;
} else if (input == 'O' || input == 'o') {
yourChar = 'O';
compChar = 'X';
} else {
System.out.println("THATS NOT 'X' OR 'O', TRY AGAIN");
in.nextLine();
}
} catch (Exception e) {
System.out.println("THATS NOT 'X' OR 'O', TRY AGAIN");
in.nextLine();
}
}
while (true) {
System.out.println("WHERE DO YOU MOVE");
// check the user can move where they want to and if so move them there
while (true) {
int input;
try {
input = in.nextInt();
if (gameBoard.getBoardValue(input) == ' ') {
gameBoard.setArr(input, yourChar);
break;
} else {
System.out.println("INVALID INPUT, TRY AGAIN");
}
in.nextLine();
} catch (Exception e) {
System.out.println("INVALID INPUT, TRY AGAIN");
in.nextLine();
}
}
gameBoard.printBoard();
System.out.println("THE COMPUTER MOVES TO");
while (true) {
int position = 1 + compChoice.nextInt(9);
if (gameBoard.getBoardValue(position) == ' ') {
gameBoard.setArr(position, compChar);
break;
}
}
gameBoard.printBoard();
// if there is a win print if player won or the computer won and ask if they
// want to play again
if (gameBoard.checkWin(yourChar)) {
System.out.println("YOU WIN, PLAY AGAIN? (Y/N)");
gameBoard.clear();
while (true) {
try {
char input;
input = in.next().charAt(0);
if (input == 'Y' || input == 'y') {
break;
} else if (input == 'N' || input == 'n') {
System.exit(0);
} else {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
} catch (Exception e) {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
}
break;
} else if (gameBoard.checkWin(compChar)) {
System.out.println("YOU LOSE, PLAY AGAIN? (Y/N)");
gameBoard.clear();
while (true) {
try {
char input;
input = in.next().charAt(0);
if (input == 'Y' || input == 'y') {
break;
} else if (input == 'N' || input == 'n') {
System.exit(0);
} else {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
} catch (Exception e) {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
}
break;
} else if (gameBoard.checkDraw()) {
System.out.println("DRAW, PLAY AGAIN? (Y/N)");
gameBoard.clear();
while (true) {
try {
char input;
input = in.next().charAt(0);
if (input == 'Y' || input == 'y') {
break;
} else if (input == 'N' || input == 'n') {
System.exit(0);
} else {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
} catch (Exception e) {
System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN");
in.nextLine();
}
}
break;
}
}
}
}
}