Java

I started programming in java in grade 11 and have loved programming ever since. Some of my Java projects include a simple platformer game similar to Archers Adventure, a RTS game similar to Command and Conquer, and Tic-tac-toe with computers that have options for different difficulty levels, and many other smaller projects.

See all of my projects in my Github Repository

RTS Game

My biggest project in high school was this RTS game that I made for my final project in grade 12 this is the selection and movement class from the game. See the full project at https://github.com/JustinVer/Command-and-Conquer-Grade-12-Final-project.

package main;

import java.util.Vector;

/**
* Class for selecting units and moving the selected units
*
* @author Justin
*
*/
public class Selection {
// Declare variables
private static Vector<Vector<Boolean>> saves = new Vector<Vector<Boolean>>();
public static Vector<Boolean> selected = new Vector<Boolean>();
public static boolean isSelected;
public static boolean tryingToSave = false;

/**
* When created create the 10 saves for the 10 number keys
*/
public Selection() {
for (int i = 0; i < 10; i++) {
saves.add(new Vector<Boolean>());
}
}

/**
* Selects all of the units in the selection box
*
* @param units The vector of all of the players units
*/
public static void Selected(Vector<Units> units) {
Selection.isSelected = false;
Selection.selected.removeAllElements();
for (int i = 0; i < units.size(); i++) {
if (DrawImages.selectionRecX < units.elementAt(i).x + units.elementAt(i).width
&& units.elementAt(i).x < DrawImages.selectionRecX + DrawImages.selectionRecW
&& units.elementAt(i).y < DrawImages.selectionRecY + DrawImages.selectionRecH
&& DrawImages.selectionRecY < units.elementAt(i).y + units.elementAt(i).height) {
Selection.selected.add(true);
Selection.isSelected = true;
} else {
Selection.selected.add(false);
}
}
}

/**
* Saves the selected units to the correct save key
*
* @param saveNum The number you would like to save the currently selected units
*                to
*/
public static void saveSelected(int saveNum) {
Selection.saves.elementAt(saveNum).removeAllElements();
for (int i = 0; i < Selection.selected.size(); i++) {
Selection.saves.elementAt(saveNum).add(Selection.selected.elementAt(i));
}
}

/**
* Selects all of the units that were saved to the key
*
* @param num The key that you would like to save from
*/
public static void getSelected(int num) {
Selection.selected.removeAllElements();
Selection.selected.addAll(Selection.saves.elementAt(num));
Selection.isSelected = true;
}

/**
* Moves the selected units to the mouse when pressed
*/
public static void move() {
int countX = 0;
int countY = 0;
int distanceLeft = 0;
int distanceRight = 0;

if (selected.size() > 0) {
for (int i = 0; i < selected.size(); i++) {
if (selected.elementAt(i).booleanValue() == true) {
// When giving move orders have the units go into a formation were the first
// unit goes in the center then alternating left and right until you reach the
// line limit and then create a new line
if (countX % 2 == 0) {
DrawImages.units.elementAt(i).moveToX = Play.mouseX + (((countX / 2) * 20)) + distanceRight;
DrawImages.units.elementAt(i).moveToY = Play.mouseY + (countY * 100);
DrawImages.units.elementAt(i).moving = true;
distanceRight = distanceRight + DrawImages.units.elementAt(i).width;
} else {
distanceLeft = distanceLeft + DrawImages.units.elementAt(i).width;
DrawImages.units.elementAt(
i).moveToX = (int) (Play.mouseX - ((((countX + 1) / 2) * 20)) - distanceLeft);
DrawImages.units.elementAt(i).moveToY = Play.mouseY + (countY * 100);
DrawImages.units.elementAt(i).moving = true;
}
countX++;
if (countX == 5) {
countY++;
countX = 0;
distanceLeft = 0;
distanceRight = 0;
}

}
}
}
}

/**
* Moves the enemy units
*/
public static void moveEnemy() {
int countX = 0;
int countY = 0;
int distanceLeft = 0;
int distanceRight = 0;
if (DrawImages.enemyUnits.size() > 0) {
for (int i = 0; i < DrawImages.enemyUnits.size(); i++) {
// When giving move orders have the enemyUnits go into a formation were the
// first
// unit goes in the center then alternating left and right until you reach the
// line limit and then create a new line
if (DrawImages.enemyUnits.elementAt(i).name.equals("HarvesterRed") == false
&& DrawImages.enemyUnits.elementAt(i).moveToX != DrawImages.buildings.elementAt(0).getX()
&& DrawImages.enemyUnits.elementAt(i).moveToY != DrawImages.buildings.elementAt(0).getY()) {
if (countX % 2 == 0) {
DrawImages.enemyUnits.elementAt(i).moveToX = Computerplayer.waitX + (((countX / 2) * 20))
+ distanceRight;
DrawImages.enemyUnits.elementAt(i).moveToY = Computerplayer.waitY + (countY * 100);
DrawImages.enemyUnits.elementAt(i).moving = true;
distanceRight = distanceRight + DrawImages.enemyUnits.elementAt(i).width;
} else {
distanceLeft = distanceLeft + DrawImages.enemyUnits.elementAt(i).width;
DrawImages.enemyUnits.elementAt(
i).moveToX = (int) (Computerplayer.waitX - ((((countX + 1) / 2) * 20)) - distanceLeft);
DrawImages.enemyUnits.elementAt(i).moveToY = Computerplayer.waitY + (countY * 100);
DrawImages.enemyUnits.elementAt(i).moving = true;
}
countX++;
if (countX == 5) {
countY++;
countX = 0;
distanceLeft = 0;
distanceRight = 0;
}
}

}
}
}
}


Tic Tac Toe

One of my favorite projects from high school is Tic-tac-toe. See the full project at JustinVer/Tic-Tac-Toe.

/* Name: Justin Verhoog
* Date: 2022-10-21
* Program Title: playTicTacToe
* Description: Play tik tac toe
*/
package main;

import java.io.*;

public class playTicTacToe {
public static void main(String args[]) throws Exception {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in), 1);
GameBoard game = new GameBoard();// makes a blank gameboard
String player1;
String player2 = "";
int turn = 0;
int index = 0;
int gameN;
boolean errorFlag;
boolean gameOver = false;
System.out.print("Please Enter Player 1's Name: ");
player1 = keyboard.readLine();
//Asks the user for the game mode they want to play
System.out.println(
"Press 1 for a 2 player game press 2 for a easy opponent press 3 for a hard opponent and press 4 for an opponent that learns");
gameN = Integer.parseInt(keyboard.readLine());
//Only asks for a second player if there is need for a second player
if (gameN == 1) {
System.out.println(player1 + " you are X's");
System.out.print("Please Enter Player 2's Name: ");
player2 = keyboard.readLine();
System.out.println(player2 + " you are O's");
}else {
System.out.println(player1 + " you are O's");
}
System.out.println("Press ENTER to continue");
keyboard.readLine();
System.out.println();
System.out.println();
game.drawBoard();
System.out.println();
System.out.println();
while (gameOver == false) {
errorFlag = false;
if (turn % 2 == 0) {// if it is player 1's turn
do {
//Depending on the game mode gets the starting players move in the correct way
if (gameN == 1) {
System.out.print(player1 + " enter your choice (1-9): ");
index = Integer.parseInt(keyboard.readLine());// Assumes a valid number is entered
}
if (gameN == 2) {
index = randomAI.move();
}
if (gameN == 3) {
index = game.playAIHard();
}
if (gameN == 4) {
index = game.playAILearn();
}
errorFlag = game.play("X", index);
if (errorFlag == false)
System.out.println("That square is already taken or invalid.  Try again");
} while (errorFlag == false);// makes sure player enters a square not yet used
System.out.println();
System.out.println();
game.drawBoard();
game.write(index, "X", game.checkWin("X"));
System.out.println();
System.out.println();
if (game.checkWin("X")) {
//if it is 2 humans playing print that player 1 has won if not print that the computer has won
if (gameN == 1) {
System.out.println(player1 + ", YOU WIN!!");
}else if(gameN == 2||gameN == 3||gameN == 4) {
System.out.println("The Computer Won");
}
gameOver = true;
}
} else {// player 2s turn

do {
System.out.print(player2 + " enter your choice (1-9): ");
index = Integer.parseInt(keyboard.readLine());// Assumes an integer is entered
errorFlag = game.play("O", index);
if (errorFlag == false)
System.out.println("That square is already taken or invalid.  Try again");
} while (errorFlag == false);// makes sure player enters a square not yet used
System.out.println();
System.out.println();
game.drawBoard();
System.out.println();
System.out.println();
if (game.checkWin("O")) {
//if it is 2 humans playing it prints that player 2 has won if not then player 1 has won
if (gameN == 1) {
System.out.println(player2 + ", YOU WIN!!");
} else {
System.out.println(player1 + ", YOU WIN!!");
}
gameOver = true;
}

}
turn++;
if (turn == 9 && gameOver == false) {
gameOver = true;
System.out.println("DRAW");
}
} // end while
game.record(game.checkWin("X"));
}// end main
}// end class