Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

card #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

card #274

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
154 changes: 154 additions & 0 deletions Java/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
public class Array {
private int count;
private String array[];

// Constructor initialzing array and its length
public Array(int length) {
this.array = new String[length];
}

// Method which prints the array
// And skips any index with val of null
public void printArray() {
for(int i =0; i < count; i++) {
// checking if the index is a valid card (not null)
if (array[i] != null) {
System.out.print(array[i] + " ");
}
}
}

public String getCard(int index) {
return array[index];
}

public int cardValue(String card) {
if (card.equals("King") != true && card.equals("Queen") != true && card.equals("Jack") != true && card.equals("Ace") != true ) {
return Integer.parseInt(card);
}
else if (card.equals("King") == true) {
return 13;
}
else if (card.equals("Queen") == true) {
return 12;
}
else if (card.equals("Jack") == true) {
return 11;
}
else if (card.equals("Ace") == true) {
return 14;
}
else {
return -1;
}
}

public int cardValue(int index) {
String card = getCard(index);
if (card.equals("King") != true && card.equals("Queen") != true && card.equals("Jack") != true && card.equals("Ace") != true ) {
return Integer.parseInt(card);
}
else if (card.equals("King") == true) {
return 13;
}
else if (card.equals("Queen") == true) {
return 12;
}
else if (card.equals("Jack") == true) {
return 11;
}
else if (card.equals("Ace") == true) {
return 14;
}
else {
return -1;
}
}

public int getLength() {
return count;
}

// Method which gets the index of a specified card
public int getIndex(String cardName) {
// loops through every element in the array
for(int i = 0; i < array.length; i++) {
// Stops when the first card equaling the search
// parameter is found
if(array[i] == cardName) {
return i;
}
}
// returns -1 if none are found
return -1;
}

// Method which adds a card onto the end of the array
// Can expand the array dynamically if needed
public void insertCard(String card) {
// checking if the length of the array is equal to
// the amount of cards in the array (count)
if (array.length == count) {
// creating a new array that's twice the size
String[] newArray = new String[count*2];
// for loop which copies the array onto a dif array
// then replaces the original array with the new one
for(int i=0; i<count; i++) {
newArray[i] = array[i];
}
array = newArray;
}
// add the card onto the end of the array, then increase
// the value of count to indicate another card is in the deck
array[count++] = card;
}

public void removeCard(String cardName) {

// Get the index of the card to be removed and mark it
array[getIndex(cardName)] = "Skip";
String[] newArray = new String[array.length-1];
int index = 0;
// loops through every card in the array
// and if it != the card it skips it
for(int i=0; i<array.length; i++) {
if (array[i] != "Skip") {
newArray[index] = array[i];
index++;
}
}
// Replace the array with the resized one
// And reduce the size of the array by 1
array = newArray;
count--;
}

// Resets the array and makes a new blank deck
public void newBlankDeck(int size) {
// Resets the array and count
String[] blankArray = new String[1];
array = blankArray;
count = 0;
// Adds each card x (size) times
for(int i =0; i<size; i++) {
insertCard("Ace");
insertCard("King");
insertCard("Queen");
insertCard("Jack");
insertCard("10");
insertCard("9");
insertCard("8");
insertCard("7");
insertCard("6");
insertCard("5");
insertCard("4");
insertCard("3");
insertCard("2");
}

}




}
163 changes: 163 additions & 0 deletions Java/Main.Java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import java.util.Scanner;
import java.util.Random;

public class Main
{
public static void main(String[] args) throws InterruptedException {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int gameLength = -1;
String textColor = "\u001B[40m\u001B[36m";
String green = "\u001B[40m\u001B[32m";
String red = "\u001B[40m\u001B[31m";
String clearLine = "\n";

// Bug where once you hit the end of the console
// The background color becomes consistent with every space
// Using that here
for(int i =0; i<50; i++) {
System.out.println(textColor+"");
}
clearScreen();

while(gameLength == -1) {
// Asks user for game length
clearScreen();
System.out.print(textColor + " Select the length of the game: \n Short Medium Long \n ");
String gameLengthChoice = scan.nextLine().toLowerCase();

// Setting the length based on the user input
if (gameLengthChoice.equals("short")) {
gameLength = 1;
}
else if (gameLengthChoice.equals("medium")) {
gameLength = 2;
}
else if (gameLengthChoice.equals("long")) {
gameLength = 3;
}
}

Array playerDeck = new Array(1);
playerDeck.newBlankDeck(gameLength);

Array computerDeck = new Array(1);
computerDeck.newBlankDeck(gameLength);
int playerCardIndex;
int computerCardIndex;

while (playerDeck.getLength() >= 1 && computerDeck.getLength() >= 1) {

playerCardIndex = rand.nextInt(playerDeck.getLength());
computerCardIndex = rand.nextInt(computerDeck.getLength());

String playerCard = playerDeck.getCard(playerCardIndex);
String computerCard = computerDeck.getCard(computerCardIndex);

if (playerDeck.cardValue(playerCard) > computerDeck.cardValue(computerCard)) {
computerDeck.removeCard(computerCard);
playerDeck.insertCard(computerCard);
System.out.print(green+" Player wins the round! \n (P) " + playerCard + " vs (C) " + computerCard);
System.out.println(" | Player Deck: " + playerDeck.getLength() + " Computer Deck: " + computerDeck.getLength() + clearLine);
Thread.sleep(1300);
}
else if (playerDeck.cardValue(playerCard) < computerDeck.cardValue(computerCard)) {
playerDeck.removeCard(playerCard);
computerDeck.insertCard(playerCard);
System.out.print(red+" Computer wins the round! \n (P) " + playerCard + " vs (C) " + computerCard);
System.out.println(" | Player Deck: " + playerDeck.getLength() + " Computer Deck: " + computerDeck.getLength() + "\n");
Thread.sleep(1300);
}
// Draw mechanic
else if (playerDeck.cardValue(playerCard) == computerDeck.cardValue(computerCard)) {

int removedCards = 0;
while (playerCard == computerCard) {
System.out.print(textColor+" Draw, this means war! ");
Thread.sleep(650);
System.out.print("Lay out 3 extra cards");
Thread.sleep(750);
System.out.print(".");
Thread.sleep(750);
System.out.print(".");
Thread.sleep(750);
System.out.println(".");
Thread.sleep(500);

removedCards += 3;
playerCardIndex = rand.nextInt(playerDeck.getLength());
computerCardIndex = rand.nextInt(computerDeck.getLength());

playerCard = playerDeck.getCard(playerCardIndex);
computerCard = computerDeck.getCard(computerCardIndex);

if (playerDeck.cardValue(playerCard) > computerDeck.cardValue(computerCard)) {
computerDeck.removeCard(computerCard);
playerDeck.insertCard(computerCard);
for(int i =0; i < removedCards; i++) {
if (computerDeck.getLength() > 0) {
computerCardIndex = rand.nextInt(computerDeck.getLength());
computerCard = computerDeck.getCard(computerCardIndex);
computerDeck.removeCard(computerCard);
playerDeck.insertCard(computerCard);
}
}
System.out.print(green+"Player wins the war!!! \n (P) " + playerCard + " vs (C) " + computerCard);
System.out.println(" | Player Deck: " + playerDeck.getLength() + " Computer Deck: " + computerDeck.getLength() + "\n");
Thread.sleep(1300);

}

else if (playerDeck.cardValue(playerCard) < computerDeck.cardValue(computerCard)) {
playerDeck.removeCard(playerCard);
computerDeck.insertCard(playerCard);

for(int i =0; i < removedCards; i++) {
if (playerDeck.getLength() > 0) {
playerCardIndex = rand.nextInt(playerDeck.getLength());
playerCard = playerDeck.getCard(playerCardIndex);
playerDeck.removeCard(playerCard);
computerDeck.insertCard(playerCard);
}
}
System.out.print(red+" Computer wins the war!!! \n (P) " + playerCard + " vs (C) " + computerCard);
System.out.println(" | Player Deck: " + playerDeck.getLength() + " Computer Deck: " + computerDeck.getLength() + "\n");
Thread.sleep(1300);
}

}
}
}

if (playerDeck.getLength() > computerDeck.getLength()) {
System.out.println("Player wins!!");
}
else if (playerDeck.getLength() < computerDeck.getLength()) {
System.out.println("Computer wins :(");
}


}

public static void sleepy() {

}

public static void clearScreen() {
System.out.print("\033[H\033[2J");
}
}




/*
Start Sequence - done
Player Setup - done
Deck setup - Done
Rounds - done but not pretty (it is now pretty)
Subtracting/adding cards - Done
Card draw condition - done
Win Condition - done
Done!!!!!!!!!!!1
*/
File renamed without changes.