Updating branch.
This commit is contained in:
parent
4c3e57769b
commit
a5671cab39
2 changed files with 110 additions and 25 deletions
BIN
Client.class
BIN
Client.class
Binary file not shown.
135
Client.java
135
Client.java
|
@ -11,25 +11,109 @@ import java.util.Scanner;
|
|||
import java.io.DataInputStream;
|
||||
import java.util.Arrays;
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.html.parser.ParserDelegator;
|
||||
import javax.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
//import javax.swing.text.html.parser.ParserDelegator;
|
||||
|
||||
public class Client {
|
||||
public class Client extends JFrame {
|
||||
|
||||
public Client() {
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public void drawBoard(String b) {
|
||||
board = b;
|
||||
bp = new BoardPanel();
|
||||
board = TrimBoard();
|
||||
JFrame frame = new JFrame("Breakthrough");
|
||||
frame.getContentPane().add(BorderLayout.CENTER, bp);
|
||||
frame.setSize(800, 600);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
bp.DrawPieces();
|
||||
/*
|
||||
Graphics test = getGraphics();
|
||||
test.clearRect(0, 0, 800, 600);
|
||||
test.setColor(Color.black);
|
||||
*/
|
||||
//frame.getContentPane().add(test, BorderLayout.CENTER);
|
||||
//this.paint(test);
|
||||
//frame.repaint();
|
||||
}
|
||||
|
||||
public String TrimBoard() {
|
||||
String output = "";
|
||||
for(int i = 0; i < board.length(); ++i) {
|
||||
if(board.charAt(i) == 'X') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
|
||||
else if(board.charAt(i) == '_') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
|
||||
else if(board.charAt(i) == 'O') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
class BoardPanel extends JPanel {
|
||||
public void DrawPieces() {
|
||||
Graphics g = getGraphics();
|
||||
g.clearRect(0, 0, 800, 600);
|
||||
g.setColor(Color.black);
|
||||
this.validate();
|
||||
//add("Center", new BoardCanvas());
|
||||
|
||||
//background.setRect(0.0, 0.0, 800.0, 600.0);
|
||||
//back = background;
|
||||
//back.setPaint(0, 0, 0);
|
||||
//g.draw(background);
|
||||
}
|
||||
}
|
||||
|
||||
class BoardCanvas extends Canvas {
|
||||
public void paint(Graphics graphics) {
|
||||
Graphics2D g = (Graphics2D) graphics;
|
||||
|
||||
Shape pieces[] = new Shape[32];
|
||||
|
||||
int c = 0;
|
||||
|
||||
Shape sh = new Ellipse2D.Double(0,0,100,100);
|
||||
g.draw(sh);
|
||||
|
||||
for(int x = 0; x < board.length(); ++x) {
|
||||
if(board.charAt(x) == 'X') {
|
||||
pieces[c] = new Ellipse2D.Double((((x%8) + 1)*10), ((x/8)*20), 10, 10);
|
||||
c++;
|
||||
}
|
||||
|
||||
else if(board.charAt(x) == 'O') {
|
||||
pieces[c] = new Ellipse2D.Double((((x%8) + 1)*10), ((x/8)*20), 10, 10);
|
||||
c++;
|
||||
}
|
||||
}
|
||||
|
||||
for(int s = 0; s < pieces.length; ++s) {
|
||||
if(pieces[s] != null) {
|
||||
g.draw(pieces[s]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main (String[] args) {
|
||||
Scanner keyboard = new Scanner(System.in);
|
||||
String hostname = args[0];
|
||||
int portnum = Integer.parseInt(args[1]);
|
||||
//keyboard.nextLine(); // used to buffer out extra space.
|
||||
ParserDelegator parserDelegator = new ParserDelegator();
|
||||
//System.out.println("parserDelegator set: "+ parserDelegator);
|
||||
JFrame frame = new JFrame("Breakthrough");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(800, 600);
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
|
||||
//frame.pack();
|
||||
frame.setVisible(true);
|
||||
Client window = new Client();
|
||||
|
||||
try {
|
||||
Socket echoSocket = new Socket(hostname, portnum);
|
||||
|
@ -40,37 +124,34 @@ public class Client {
|
|||
String userInput = "test";
|
||||
char[] b = new char[256];
|
||||
in.read(b, 0, 256);
|
||||
String temp = "<html><b>" + String.valueOf(b).replace("\n", "<br>").trim() + "</b></html>";
|
||||
JLabel label = new JLabel("<html>testing<html>");
|
||||
frame.getContentPane().add(label);
|
||||
System.out.print(b);
|
||||
|
||||
String temp = String.valueOf(b).trim();
|
||||
System.out.print(temp);
|
||||
//setBoard(temp);
|
||||
//window.drawBoard(temp);
|
||||
|
||||
String g = "GAME OVER";
|
||||
char[] go = new char[9];
|
||||
for(int x = 0; x < go.length; ++x) {
|
||||
go[x] = g.charAt(x);
|
||||
}
|
||||
|
||||
boolean end = false;
|
||||
int c = 0;
|
||||
|
||||
while(!end && (userInput != null)) {
|
||||
userInput = stdIn.readLine();
|
||||
output.println(userInput);
|
||||
output.flush();
|
||||
char[] buffer = new char[256];
|
||||
in.read(buffer, 0, 256);
|
||||
JLabel display = new JLabel(Arrays.toString(buffer));
|
||||
if(c == 0) {
|
||||
frame.getContentPane().remove(label);
|
||||
++c;
|
||||
}
|
||||
else {
|
||||
frame.getContentPane().remove(display);
|
||||
}
|
||||
frame.getContentPane().add(display);
|
||||
for(int i = 0; i < buffer.length; ++i) {
|
||||
if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
|
||||
end = true;
|
||||
}
|
||||
}
|
||||
System.out.println(String.valueOf(buffer).trim());
|
||||
window.drawBoard(String.valueOf(buffer).trim());
|
||||
}
|
||||
output.close();
|
||||
in.close();
|
||||
|
@ -82,4 +163,8 @@ public class Client {
|
|||
System.err.println("IOException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
String board;
|
||||
|
||||
BoardPanel bp;
|
||||
}
|
Reference in a new issue