Last active
February 19, 2020 02:10
-
-
Save feehe21/83220e35c653a7222c6f057728fd00c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class card | |
{ | |
private int value; | |
public card(int v){ | |
this.value = v; | |
} | |
public int getValue(){ | |
return this.value; | |
} | |
public void setValue(int n){ | |
this.value = n; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class hand | |
{ | |
pile h; | |
int pri1; | |
int pri2; | |
public hand(int prio1, int prio2){ | |
this.h = new pile(); | |
this.pri1 = prio1; | |
this.pri2 = prio2; | |
} | |
public void add(card n){ | |
h.add(n); | |
} | |
public int getFinal(){ | |
return h.get(h.getSize()-1); | |
} | |
public void print(){ | |
h.print(); | |
} | |
public card remove(){ | |
System.out.print("removing a card..."); | |
if(h == null){ | |
System.out.println("it was null"); | |
return null; | |
} | |
card hold = new card(8); | |
int n1 = 0; | |
int n2 = 0; | |
int spot1 = -1; | |
int spot2 = -1; | |
for(int i = 0; i < h.getSize(); i++){ | |
if(h.get(i)!=pri1 &&h.get(i)!=pri2){ | |
System.out.println("c" + i); | |
hold = h.remove(i); | |
return hold; | |
} | |
if(h.get(i)==pri1){ | |
n1++; | |
spot1 = i; | |
} | |
if(h.get(i)==pri2){ | |
n2++; | |
spot2 = i; | |
} | |
} | |
if(n1>n2 && n2 > 0){ | |
System.out.println("x"); | |
hold = h.remove(spot2); | |
}else if(n2>n1 && n1 > 0){ | |
System.out.println("s"); | |
hold = h.remove(spot1); | |
}else{ | |
System.out.println("e"); | |
hold = h.remove(0); | |
} | |
return hold; | |
} | |
public boolean hasWon(){ | |
if(h.get(0)==h.get(1) && h.get(0)==h.get(2) &&h.get(0)==h.get(3)){ | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import java.io.*; | |
// Java program to implement | |
// a Singly Linked List | |
public class LinkedList <V> { | |
Node head; // head of list | |
// Method to insert a new node | |
public LinkedList insert(LinkedList list, card data) | |
{ | |
// Create a new node with given data | |
Node new_node = new Node(data); | |
new_node.next = null; | |
// If the Linked List is empty, | |
// then make the new node as head | |
if (list.head == null) { | |
list.head = new_node; | |
} | |
else { | |
// Else traverse till the last node | |
// and insert the new_node there | |
Node last = list.head; | |
while (last.next != null) { | |
last = last.next; | |
} | |
// Insert the new_node at last node | |
last.next = new_node; | |
} | |
// Return the list by head | |
return list; | |
} | |
public LinkedList set(LinkedList list, int spot, int value){ | |
if (spot >= getSize(list)) { | |
return null; | |
}else { | |
// Else traverse till the last node | |
// and insert the new_node there | |
Node last = list.head; | |
for(int i = 0; i < spot-1; i++){ | |
last = last.next; | |
} | |
last.data.setValue(value); | |
} | |
// Return the list by head | |
return list; | |
} | |
// removes the element at the specified position in this list. | |
public int remove(LinkedList list,int index) { | |
// if the index is out of range, exit | |
if (index < 0){// || index > getSize(list)) | |
System.out.println("bad1"); | |
return -1; | |
} | |
Node currNode = list.head; | |
Node lost; | |
if (head != null) { | |
if(index == 0){ | |
lost = list.head; | |
list.head = list.head.getNext(); | |
}else{ | |
for (int i = 0; i < index-1; i++) { | |
if (currNode.getNext() == null){ | |
System.out.println("bad2"); | |
return -1;} | |
currNode = currNode.getNext(); | |
} | |
//System.out.println("data: " + currNode.data); | |
lost = currNode.getNext(); | |
currNode.setNext(currNode.getNext().getNext()); | |
// decrement the number of elements variable | |
} | |
return lost.data.getValue(); | |
} | |
System.out.println("bad3"); | |
return -1; | |
} | |
public int getSize(LinkedList list){ | |
if (list.head == null) { | |
return 0; | |
} | |
Node last = list.head; | |
int count = 1; | |
while (last.next != null) { | |
last = last.next; | |
count++; | |
} | |
return count; | |
} | |
//addfront delfront | |
public int delFront(LinkedList list){ | |
Node hold = null; | |
if(list.head!=null){ | |
if(list.head.next!=null){ | |
hold = list.head; | |
list.head=list.head.next; | |
}else{ | |
hold = list.head; | |
list.head=null; | |
} | |
} | |
card a = (card)hold.data; | |
return (int)a.getValue(); | |
} | |
public void addFront(LinkedList list, card a){ | |
Node new_node = new Node(a); | |
if(list.head!=null){ | |
Node pastHead = list.head; | |
new_node.next=pastHead; | |
list.head=new_node; | |
}else{ | |
list.head=new_node; | |
} | |
} | |
public V get(LinkedList list, int pos){ | |
int count = 0; | |
boolean go = true; | |
if(list.head!=null){ | |
Node last = list.head; | |
for(int i =0; i<=pos; i++){ | |
if(i==pos){ | |
return (V)last.data; | |
} | |
if(last.next==null){ | |
return null; | |
} | |
last=last.next; | |
} | |
} | |
return null; | |
} | |
public int find(LinkedList list, V val){ | |
int count = 0; | |
if(list.head!=null){ | |
Node last = list.head; | |
while(last.next != null){ | |
if(val.equals(last.data)){ | |
return count; | |
} | |
last= last.next; | |
count++; | |
} | |
} | |
return -1; | |
} | |
public void printList(LinkedList list){ | |
if(list.head!=null){ | |
Node last = list.head; | |
System.out.println("List: "); | |
while (last.next != null) { | |
System.out.print(last.data+" "); | |
last = last.next; | |
} | |
System.out.print(last.data); | |
}else{ | |
System.out.println("List empty"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class main | |
{ | |
public static ArrayList<player> pls; | |
public static ArrayList<pile> pis; | |
String won = null; | |
public static int playerTurn; | |
public static boolean someonesWon; | |
public static ThreadTest zero; | |
public static ThreadTest one; | |
public static ThreadTest two; | |
public static ThreadTest three; | |
static boolean first = true; | |
public static void setup() | |
{ | |
pls = new ArrayList<player>(); | |
pis = new ArrayList<pile>(); | |
playerTurn = 0; | |
someonesWon = false; | |
for(int i = 0; i<4; i++){ | |
pls.add(new player(i)); | |
pis.add(new pile()); | |
} | |
giveOutCards(); | |
zero = new ThreadTest(); | |
one = new ThreadTest(); | |
two = new ThreadTest(); | |
three = new ThreadTest(); | |
zero.setPos(0); | |
one.setPos(1); | |
two.setPos(2); | |
three.setPos(3); | |
zero.start(); | |
one.start(); | |
two.start(); | |
three.start(); | |
} | |
public static void main(String[] args){ | |
if(first){ | |
first = false; | |
setup(); | |
} | |
} | |
public static void giveOutCards(){ | |
ArrayList<Integer> a = new ArrayList<Integer>(); | |
for(int i = 0; i < 4; i++){ | |
for(int n = 0; n < 8; n++){ | |
a.add(n); | |
} | |
} | |
int rand = 0; | |
for(int i = 0; i < 32; i++){ | |
rand = (int)(Math.random()*a.size()); | |
if(i<16){ | |
pls.get(i%4).addToHand(a.remove(rand)); | |
//System.out.print("pl"); | |
}else{ | |
pis.get(i%4).add(a.remove(rand)); | |
//System.out.print("pi"); | |
} | |
} | |
for(int i = 0; i < 4; i++){ | |
//System.out.println("Player " + i); | |
pls.get(i).printHand(); | |
//System.out.println("Pile " + i); | |
pis.get(i).print(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Node | |
{ | |
public card data; | |
Node next; | |
Node(card d){ | |
data = d; | |
next = null; | |
} | |
public Node getNext() { | |
return next; | |
} | |
public void setNext(Node nextValue) { | |
next = nextValue; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class pile | |
{ | |
LinkedList p; | |
public pile(){ | |
this.p = new LinkedList(); | |
} | |
public int getSize(){ | |
return p.getSize(p); | |
} | |
public void print(){ | |
for(int i = 0; i < getSize();i++){ | |
System.out.print(get(i) + " "); | |
} | |
System.out.println(" a"); | |
} | |
public int get(int spot){ | |
card a = (card)p.get(p,spot); | |
return (int)a.getValue(); | |
} | |
public void add(card n){ | |
p.insert(p,n); | |
} | |
public void add(int n){ | |
card q = new card(n); | |
p.insert(p,q); | |
} | |
public card remove(){ | |
card n = new card(p.delFront(p)); | |
return n; | |
} | |
public card remove(int spot){ | |
//System.out.println("removing spot " + spot); | |
card q = new card(p.remove(p,spot)); | |
//System.out.println("the removed card's number was "+q.getValue()); | |
//card n = new card((p.remove(p, spot).data)); | |
//return n; | |
return q; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class player | |
{ | |
public int spot; | |
public int nextSpot; | |
public hand h; | |
int priority1; | |
int priority2; | |
boolean won = false; | |
public player(int s) | |
{ | |
this.spot = s; | |
nextSpot = spot+1; | |
if(nextSpot > 3){ | |
nextSpot = 0; | |
} | |
priority1 = spot*2; | |
priority2 = priority1+1; | |
//0-0/1 1-2/3 2-4/5 3-6/7 | |
h = new hand(priority1, priority2); | |
} | |
public void addToHand(int a){ | |
card q = new card(a); | |
h.add(q); | |
} | |
public void printHand(){ | |
h.print(); | |
} | |
public void test(){ | |
System.out.println("go" + spot + nextSpot); | |
} | |
public synchronized boolean turn(){ | |
System.out.println("\n" +"Player "+spot+"'s turn"); | |
h.add(main.pis.get(spot).remove()); | |
System.out.println("Player "+spot+" drew " + h.getFinal()); | |
h.print(); | |
//card hold = h.remove(priority1, priority2); | |
card hold = h.remove(); | |
main.pis.get(nextSpot).add(hold); | |
if(hold == null){ | |
System.out.println("b"); | |
} | |
System.out.println("Player "+ spot + " discared " + hold.getValue()); | |
h.print(); | |
System.out.println("piles"); | |
main.pis.get(spot).print(); | |
main.pis.get(nextSpot).print(); | |
main.playerTurn++; | |
if(h.hasWon()){ | |
System.out.println("player " + spot + " has won!!!!!!!!!!!!!!"); | |
main.someonesWon = true; | |
won = true; | |
return true; | |
} | |
return false; | |
} | |
public synchronized void endgame(){ | |
System.out.println("Player " + spot+"'s cards:"); | |
h.print(); | |
System.out.println("Pile" + spot+"'s contains:"); | |
main.pis.get(spot).print(); | |
System.out.println("Player "+spot+" won? -"+ won); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.*; | |
public class ThreadTest extends Thread | |
{ | |
int pos; | |
public void run () | |
{ | |
setName(pos + " Thread"); | |
main.pls.get(pos).test(); | |
while(!main.someonesWon){ | |
if(main.playerTurn %4 == pos){ | |
main.pls.get(pos).turn(); | |
} | |
} | |
main.pls.get(pos).endgame(); | |
} | |
public void setPos(int x){ | |
this.pos = x; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment