revamped card system

This commit is contained in:
bMorgan17 2019-08-24 11:19:11 -06:00
parent 0c6ae26e93
commit 3ac717a452
6 changed files with 80 additions and 138 deletions

View file

@ -1,12 +1,13 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
public class GridDialog {
private JDialog dialog = new JDialog();
private JTextField name1 = new JTextField("Player 1"), name2 = new JTextField("Player 2");
private SpinnerModel cards = new SpinnerNumberModel(12, 6, 100, 2);
private SpinnerModel cards = new SpinnerNumberModel(12, 6, (new File("icons").list().length * 2) - 2, 2);
private JSpinner numCards = new JSpinner(cards);
public GridDialog(String str1, String str2, int cards) {

View file

@ -7,6 +7,9 @@ import javax.swing.border.EtchedBorder;
import java.util.Random;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
int rows, cols;
@ -18,9 +21,7 @@ public class Main {
Random rand = new Random();
String labelNames[] = {"Car", "Elephant", "Phone", "Tree", "Person", "Computer", "Toy", "Paper", "Book", "Desk", "Bed", "Glasses", "Keyboard", "Ring", "Fan", "Sock", "Sign", "Eraser",
"Pencil", "Bow", "Cat", "Boat", "Fork", "Spoon", "Door", "Clock", "Bottle", "Purse", "Brush", "Camera", "Money", "Bread", "Screw", "Mirror", "Cork", "Sponge",
"Banana", "Bowl", "Tomato", "Key", "Nail", "Hammer", "Chain", "Table", "Towel", "Cord", "Thread", "Chalk", "Baby", "Candy"};
String labelNames[] = new String[(new File("icons").list().length * 2) - 2];
JPanel panels[][];
JButton btns[][];
@ -54,6 +55,24 @@ public class Main {
ex.printStackTrace();
}*/
File dir = new File("icons");
File[] directoryListing = dir.listFiles();
String fileName;
int extras = 0;
List<File> temp = Arrays.asList(directoryListing);
Collections.shuffle(temp);
directoryListing = (File[])temp.toArray();
for (int i = 0; i < directoryListing.length; i++) {
fileName = directoryListing[i].getName();
if (fileName.endsWith(".png")) {
fileName = fileName.substring(0, fileName.length() - 4);
fileName = fileName.substring(0, 1).toUpperCase() + fileName.substring(1);
labelNames[i - extras] = fileName;
} else extras++;
}
rows = r;
cols = c;
@ -75,7 +94,7 @@ public class Main {
});
try {
player1Turn = new JLabel(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player1Turn = new JLabel(new ImageIcon(ImageIO.read(new File("dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player2Turn = new JLabel(new ImageIcon());
} catch (IOException e) {
e.printStackTrace();
@ -100,6 +119,7 @@ public class Main {
JLabel testFont = new JLabel();
for (int i = 0; i < (rows*cols)/2; i++) {
System.out.println(labelNames[i]);
if (testFont.getFontMetrics(testFont.getFont()).stringWidth(labelNames[i]) > largestWord) largestWord = testFont.getFontMetrics(testFont.getFont()).stringWidth(labelNames[i]);
}
@ -316,14 +336,14 @@ public class Main {
if (turn == 1) {
try {
player1Turn.setIcon(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player1Turn.setIcon(new ImageIcon(ImageIO.read(new File("dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player2Turn.setIcon(new ImageIcon());
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
player2Turn.setIcon(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player2Turn.setIcon(new ImageIcon(ImageIO.read(new File("dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
player1Turn.setIcon(new ImageIcon());
} catch (IOException e) {
e.printStackTrace();

BIN
Memory.jar Normal file

Binary file not shown.

View file

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

Before After
Before After

52
icons/desktop.ini Normal file
View file

@ -0,0 +1,52 @@
[LocalizedFileNames]
car.png=@car.png,0
dot.png=@dot.png,0
candy.png=@candy.png,0
baby.png=@baby.png,0
chalk.png=@chalk.png,0
thread.png=@thread.png,0
cord.png=@cord.png,0
towel.png=@towel.png,0
table.png=@table.png,0
chain.png=@chain.png,0
hammer.png=@hammer.png,0
nail.png=@nail.png,0
key.png=@key.png,0
tomato.png=@tomato.png,0
bowl.png=@bowl.png,0
banana.png=@banana.png,0
sponge.png=@sponge.png,0
cork.png=@cork.png,0
mirror.png=@mirror.png,0
screw.png=@screw.png,0
bread.png=@bread.png,0
money.png=@money.png,0
camera.png=@camera.png,0
brush.png=@brush.png,0
purse.png=@purse.png,0
bottle.png=@bottle.png,0
clock.png=@clock.png,0
door.png=@door.png,0
spoon.png=@spoon.png,0
fork.png=@fork.png,0
boat.png=@boat.png,0
cat.png=@cat.png,0
sign.png=@sign.png,0
eraser.png=@eraser.png,0
fan.png=@fan.png,0
sock.png=@sock.png,0
ring.png=@ring.png,0
keyboard.png=@keyboard.png,0
bow.png=@bow.png,0
pencil.png=@pencil.png,0
glasses.png=@glasses.png,0
bed.png=@bed.png,0
desk.png=@desk.png,0
book.png=@book.png,0
paper.png=@paper.png,0
toy.png=@toy.png,0
computer.png=@computer.png,0
person.png=@person.png,0
tree.png=@tree.png,0
phone.png=@phone.png,0
elephant.png=@elephant.png,0

View file

@ -1,131 +0,0 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class gameServer {
public static void main(String[] args) throws Exception {
ServerSocket listener = new ServerSocket(8901);
System.out.println("Game Server is Running");
try {
while (true) {
Game game = new Game();
Game.Player player1 = game.new Player(listener.accept());
Game.Player player2 = game.new Player(listener.accept());
player1.setOpponent(player2);
player2.setOpponent(player1);
game.currentPlayer = player1;
player1.start();
player2.start();
}
} finally {
listener.close();
}
}
}
class Game {
Player currentPlayer;
// winner
public boolean hasWinner() {
return
(board[0] != null && board[0] == board[1] && board[0] == board[2])
||(board[3] != null && board[3] == board[4] && board[3] == board[5])
||(board[6] != null && board[6] == board[7] && board[6] == board[8])
||(board[0] != null && board[0] == board[3] && board[0] == board[6])
||(board[1] != null && board[1] == board[4] && board[1] == board[7])
||(board[2] != null && board[2] == board[5] && board[2] == board[8])
||(board[0] != null && board[0] == board[4] && board[0] == board[8])
||(board[2] != null && board[2] == board[4] && board[2] == board[6]);
}
// no empty squares
public boolean boardFilledUp() {
for (int i = 0; i < board.length; i++) {
if (board[i] == null) {
return false;
}
}
return true;
}
// thread when player tries a move
public synchronized boolean legalMove(int location, Player player) {
if (player == currentPlayer && board[location] == null) {
board[location] = currentPlayer;
currentPlayer = currentPlayer.opponent;
currentPlayer.otherPlayerMoved(location);
return true;
}
return false;
}
class Player extends Thread {
char mark;
Player opponent;
Socket socket;
BufferedReader input;
PrintWriter output;
// thread handler to initialize stream fields
public Player(Socket socket) {
this.socket = socket;
this.mark = mark;
try {
input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream(), true);
output.println("WELCOME " + mark);
output.println("MESSAGE Waiting for opponent to connect");
} catch (IOException e) {
System.out.println("Player died: " + e);
}
}
//Accepts notification of who the opponent is.
public void setOpponent(Player opponent) {
this.opponent = opponent;
}
//Handles the otherPlayerMoved message.
public void otherPlayerMoved(int location) {
output.println("OPPONENT_MOVED " + location);
output.println(
hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : "");
}
public void run() {
try {
// The thread is only started after everyone connects.
output.println("MESSAGE All players connected");
// Tell the first player that it is his/her turn.
if (mark == 'X') {
output.println("MESSAGE Your move");
}
// Repeatedly get commands from the client and process them.
while (true) {
String command = input.readLine();
if (command.startsWith("MOVE")) {
int location = Integer.parseInt(command.substring(5));
if (legalMove(location, this)) {
output.println("VALID_MOVE");
output.println(hasWinner() ? "VICTORY"
: boardFilledUp() ? "TIE"
: "");
} else {
output.println("MESSAGE ?");
}
} else if (command.startsWith("QUIT")) {
return;
}
}
} catch (IOException e) {
System.out.println("Player died: " + e);
} finally {
try {socket.close();} catch (IOException e) {}
}
}
}
}