This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
breakthroughpine64backup/Client.java
Alexander Huddleston a5671cab39 Updating branch.
2015-10-29 13:32:48 -05:00

170 lines
No EOL
4.1 KiB
Java
Executable file

// 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;
import java.util.Arrays;
import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.geom.*;
//import javax.swing.text.html.parser.ParserDelegator;
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]);
Client window = new Client();
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));
String userInput = "test";
char[] b = new char[256];
in.read(b, 0, 256);
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);
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();
stdIn.close();
echoSocket.close();
}
catch (IOException e){
System.err.println("IOException: " + e.getMessage());
}
}
String board;
BoardPanel bp;
}