2015-10-28 15:08:07 -05:00
|
|
|
// Alex Huddleston
|
|
|
|
// Breakthrough Client in Java
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.io.DataInputStream;
|
2015-10-28 15:48:29 -05:00
|
|
|
import java.util.Arrays;
|
2015-10-29 09:40:51 -05:00
|
|
|
import javax.swing.*;
|
2015-10-29 13:32:48 -05:00
|
|
|
import javax.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.geom.*;
|
|
|
|
//import javax.swing.text.html.parser.ParserDelegator;
|
2015-10-28 15:08:07 -05:00
|
|
|
|
2015-10-29 13:32:48 -05:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 09:40:51 -05:00
|
|
|
|
2015-10-28 15:08:07 -05:00
|
|
|
public static void main (String[] args) {
|
|
|
|
Scanner keyboard = new Scanner(System.in);
|
|
|
|
String hostname = args[0];
|
|
|
|
int portnum = Integer.parseInt(args[1]);
|
2015-10-29 09:40:51 -05:00
|
|
|
|
2015-10-29 13:32:48 -05:00
|
|
|
Client window = new Client();
|
2015-10-28 15:08:07 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
Socket echoSocket = new Socket(hostname, portnum);
|
|
|
|
PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true);
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
|
|
|
|
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
|
2015-10-29 09:40:51 -05:00
|
|
|
String userInput = "test";
|
2015-10-28 15:08:07 -05:00
|
|
|
char[] b = new char[256];
|
|
|
|
in.read(b, 0, 256);
|
2015-10-29 13:32:48 -05:00
|
|
|
|
|
|
|
String temp = String.valueOf(b).trim();
|
|
|
|
System.out.print(temp);
|
|
|
|
//setBoard(temp);
|
|
|
|
//window.drawBoard(temp);
|
|
|
|
|
2015-10-29 09:40:51 -05:00
|
|
|
String g = "GAME OVER";
|
|
|
|
char[] go = new char[9];
|
|
|
|
for(int x = 0; x < go.length; ++x) {
|
|
|
|
go[x] = g.charAt(x);
|
|
|
|
}
|
2015-10-29 13:32:48 -05:00
|
|
|
|
2015-10-29 09:40:51 -05:00
|
|
|
boolean end = false;
|
|
|
|
int c = 0;
|
2015-10-29 13:32:48 -05:00
|
|
|
|
2015-10-29 09:40:51 -05:00
|
|
|
while(!end && (userInput != null)) {
|
|
|
|
userInput = stdIn.readLine();
|
2015-10-28 15:08:07 -05:00
|
|
|
output.println(userInput);
|
|
|
|
output.flush();
|
|
|
|
char[] buffer = new char[256];
|
|
|
|
in.read(buffer, 0, 256);
|
2015-10-29 09:40:51 -05:00
|
|
|
for(int i = 0; i < buffer.length; ++i) {
|
|
|
|
if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
|
|
|
|
end = true;
|
2015-10-28 15:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-29 13:32:48 -05:00
|
|
|
System.out.println(String.valueOf(buffer).trim());
|
|
|
|
window.drawBoard(String.valueOf(buffer).trim());
|
2015-10-28 15:08:07 -05:00
|
|
|
}
|
2015-10-28 15:48:29 -05:00
|
|
|
output.close();
|
|
|
|
in.close();
|
|
|
|
stdIn.close();
|
|
|
|
echoSocket.close();
|
2015-10-28 15:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (IOException e){
|
|
|
|
System.err.println("IOException: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 13:32:48 -05:00
|
|
|
|
|
|
|
String board;
|
|
|
|
|
|
|
|
BoardPanel bp;
|
2015-10-28 15:08:07 -05:00
|
|
|
}
|