d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Card Game: War > Create Card Game War
Add Reply New Topic New Poll
Member
Posts: 2
Joined: Sep 14 2015
Gold: 0.00
Sep 14 2015 07:56pm
Really just stuck completely with my code at this point and have no idea how to go about doing this. Iv'e tried using arrays and then using linked list both coming up empty. If someone can just put up a code that I can use as a template to better understand where to go next it would be greatly appreciated.

Basically I need the program to read 52 inputs (the 52 cards that would be in a deck) and then say which player would win and in how many rounds. After 10,000 rounds there should be a draw. Upon input of the deck, player 1 would get the first card entered, then player 2, so on so on until each player has 26 cards and the game then starts.

Again any code that you can give for me to go through and look at as reference is helpful. Iv'e just gotten to a point of total frustration with this project.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 14 2015 07:59pm
Why do you need to read the inputs? You already know what the cards are.

What's the assignment language?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 14 2015 08:09pm
Quote (KrzaQ2 @ Sep 14 2015 09:59pm)
Why do you need to read the inputs? You already know what the cards are.


i assume the user enters the order the cards appear in some way
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 14 2015 08:19pm
Strangely enough, I have code in my eclipse workspace for a War game that meets this exact set of criteria. I wrote it with a student on here a couple years ago. I wonder if you two went to the same school.

Unfortunately, if I was to give it to you, i would have to...alter it a bit.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 14 2015 08:23pm
Quote (Minkomonster @ Sep 14 2015 10:19pm)
Strangely enough, I have code in my eclipse workspace for a War game that meets this exact set of criteria. I wrote it with a student on here a couple years ago. I wonder if you two went to the same school.

Unfortunately, if I was to give it to you, i would have to...alter it a bit.


shame on you for using obscenities for variable names
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 14 2015 08:29pm
Quote (carteblanche @ Sep 14 2015 09:23pm)
shame on you for using obscenities for variable names


Oh no, this code is actually legit. The dude was a student who already had done the legwork, and needed help understanding a few things. I actually worked with him a bit off and on through the year. He had me on skype. He asked me after his assignment was turned in how he could write it better using objects and such. So, I screen shared with him, and took him through my thought process as I designed each component of the game as an object, and then coded it right there in front of him to give him an idea of how I tackled the problem. In other words, there is no troll code in this one.

If I was to give it to this dude...I would have to fix that last part.
Member
Posts: 2
Joined: Sep 14 2015
Gold: 0.00
Sep 15 2015 07:08am
import java.util.Scanner;

public class SinglyLinkedList<E> {

public void main(String[]args) {
// output message
System.out.println("Please input the shuffled deck in order starting from the top: ");

// get input from user
Scanner cards = new Scanner(System.in);

SinglyLinkedList player1 = new SinglyLinkedList();
SinglyLinkedList player2 = new SinglyLinkedList();
SinglyLinkedList temp = new SinglyLinkedList();

// add first element to the linked lists
player1.addFirst(cards.nextLine());
player2.addFirst(cards.nextLine());

// build initial hands
for (int k = 1; k < 26; k++) {
player1.addLast(cards.nextLine());
player2.addLast(cards.nextLine());
}

//create an array with strings to compare to the elements
String[] cardValue = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

int counter = 0;
int card1 = 0;
int card2 = 0;
while (counter <= 10000){
counter ++;

if (player1.first() == null) {
System.out.println("Player 2 wins after " + counter + " rounds.");
break;
}
if (player2.first() == null) {
System.out.println("Player 1 wins after " + counter + " rounds.");
break;
}

for (int n = 0; n < 13; n++) {
if(player1.first() == cardValue[n])
card1 = n;
if(player2.first() == cardValue[n])
card2 = n;
}

if (card1 < card2) {
// player2 wins
player2.addLast(player1.first());
player2.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
} else if (card2 < card1) {
// player1 wins
player1.addLast(player1.first());
player1.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
} else if (card1 == card2) {

// put the equal card in the temp list and remove from the players hand
temp.addLast(card1);
temp.addLast(card2);
player1.removeFirst();
player2.removeFirst();

// Check to see if any hands are null
if (player1.first() == null){
System.out.println("Player 2 wins after " + counter + " rounds.");
break;
}
else if (player2.first() == null){
System.out.println("Player 1 wins after " + counter + " rounds.");
break;
}
else {
// put the face down cards in the temp list and remove from the players hands
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
}

// assign values to the cards
for (int n = 0; n < 13; n++) {
if(player1.first().equals(cardValue[n]))
card1 = n;
if(player2.first().equals(cardValue[n]))
card2 = n;
}

if (card1 < card2) {
// player2 wins
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
while (temp.first() != null)
player2.addLast(temp.first());
temp.removeFirst();
}

} else if (card2 < card1) {
// player1 wins
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
while (temp.first() != null){
player1.addLast(temp.first());
temp.removeFirst();
}


}
if (counter == 10000) {
System.out.println("There was a draw after 10,000 rounds.");
}
}
}

static class Node<E> {
private E element;
private Node<E> nextNode;

public Node(E e, Node<E> n){ //define current node
element = e; //define element stored in node
nextNode = n; //define next node in list
}

public E getElement(){ //return the element stored in the current node
return element;
}

public Node<E> getNextNode() { //return the reference location of the next node
return nextNode;
}

public void setNextNode(Node<E> n){ //set the new reference location of the next node to input "n"
nextNode = n;
}

}

private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;

public SinglyLinkedList() {}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public E first() {
if (isEmpty())
return null;
return head.getElement();
}

public E last() {
if (isEmpty())
return null;
return tail.getElement();
}

public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size ++;
}

public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if (isEmpty())
head = newest;
else
tail.setNextNode(newest);
tail = newest;
size ++;
}

public E removeFirst() {
if (isEmpty())
return null;
E answer = head.getElement();
head = head.getNextNode();
size--;
if (size == 0)
tail = null;
return answer;
}

}

That's my actual code... I am pretty sure an issue is resulting when I try and compare them but the program isn't outputting anything... It just says it is terminating
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll