Commit 5ad9f2e8 authored by amir's avatar amir

Scanning in graphic version

parent 5a06e374
package com.company;
import java.util.Scanner;
public class Console extends Display {
public Console(byte displayType) {
super(displayType);
}
@Override
public void display(int[][] a1, int[][] a2) {
super.display(a1, a2);
System.out.println(" A B C D E F G H");
for (int i = 1; i <= 8; i++) {
System.out.print(i + " ");
for (int j = 1; j <= 8; j++) {
if (a2[j][i] != 0) {
// the unicode of white circle (there is a player2 disc)
......@@ -30,4 +32,16 @@ public class Console extends Display {
System.out.println();
}
}
@Override
public int[] getInput() {
Scanner scanner = new Scanner(System.in);
String nl = scanner.nextLine();
char[] f = nl.toCharArray();
int [] inputs = new int[2];
// scanning the x and y coordinates from user
inputs[1] = f[0] - '1' + 1;
inputs[0] = f[2] - 'A' + 1;
return inputs;
}
}
......@@ -14,4 +14,9 @@ public class Display {
public void display(int[][] a1, int[][] a2){
}
public int[] getInput(){
int[] input = new int[2];
return input;
}
}
......@@ -82,7 +82,10 @@ public class Graphic extends Display {
l1 = new JLabel(new ImageIcon("untitled.png"));
f.setSize(700, 700);
f.setLayout(null);
f.setVisible(true);
if (displayType == 0)
f.setVisible(false);
else
f.setVisible(true);
f.add(b);
b.setBounds(300,621,100, 40);//x axis, y axis, width, height
b.addActionListener(new ActionListener() {
......@@ -327,4 +330,22 @@ public class Graphic extends Display {
}
return null;
}
@Override
public int[] getInput() {
int [] ints = new int[2];
Iterator<JButton> buttonIterator = buttons.iterator();
while (buttonIterator.hasNext()) {
//System.out.println("koft!");
JButton b1 = buttonIterator.next();
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ints[0] = (b1.getX() - 143) / 58 + 1;
ints[1] = (b1.getY() - 135) / 58 + 1;
}
});
}
return ints;
}
}
package com.company;
import java.util.Scanner;
public class Main {
......@@ -11,15 +13,15 @@ public class Main {
byte displayType = scanner.nextByte();
Player1 play1 = new Player1();
Player2 play2 = new Player2();
Console console = new Console(displayType);
Graphic graphic = new Graphic(displayType);
if (displayType == 0) {
Console console = new Console(displayType);
console.display(play1.getArr1(), play2.getArr2());
}
else {
Graphic graphic = new Graphic(displayType);
graphic.display(play1.getArr1(), play2.getArr2());
}
/* while ((play1.getNumberOfDisks(play1.getArr1()) + play2.getNumberOfDisks(play2.getArr2()) < 64) &&
while ((play1.getNumberOfDisks(play1.getArr1()) + play2.getNumberOfDisks(play2.getArr2()) < 64) &&
(play1.getNumberOfDisks(play1.getArr1()) != 0) && (play2.getNumberOfDisks(play2.getArr2()) != 0))
{
if ((play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) == 0) &&
......@@ -28,6 +30,7 @@ public class Main {
turns++;
if (turns %2 == 1){
if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) {
if ()
System.out.println("Player1, it's your turn!");
}
else {
......@@ -37,19 +40,26 @@ public class Main {
}
}
if (turns %2 == 0){
if (play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) != 0)
System.out.println("Player2, it's your turn!");
if (play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) != 0) {
//System.out.println("Player2, it's your turn!");
}
else {
//if there is no possible move prints pass
System.out.println("pass");
continue;
}
}
String nl = scanner.nextLine();
char[] f = nl.toCharArray();
// scanning the x and y coordinates from user
int y = f[0] - '1' + 1;
int x = f[2] - 'A' + 1;
int[] inputs = new int[2];
if (displayType == 0) {
inputs = console.getInput();
}
else {
inputs = graphic.getInput();
}
int y = inputs[1];
int x = inputs[0];
System.out.println("x = " + x);
System.out.println("y = " + y);
if ((x >= 1) && (x < 9) && (y >= 1) && (y < 9)) {
if ((!play1.containsInArray(play1.getArr1(), x, y)) && (!play2.containsInArray(play2.getArr2(), x, y))){
if (turns %2 == 1){
......@@ -71,7 +81,7 @@ public class Main {
if (play1.getNumberOfDisks(play1.getArr1()) > play2.getNumberOfDisks(play2.getArr2()))
System.out.println("Player1 wins!");
else
System.out.println("Player2 wins!");*/
System.out.println("Player2 wins!");
}
......
......@@ -32,6 +32,35 @@ public class Players {
return false;
}
/**
* displays the place of black and white discs and blanks (will be overridden in console and graphic version)
* @param a1 array of the player1's discs
* @param a2 array of the player2's discs
*/
public void display(int[][] a1, int[][] a2) {
System.out.println(" A B C D E F G H");
for (int i = 1; i <= 8; i++) {
System.out.print(i + " ");
for (int j = 1; j <= 8; j++) {
if (a2[j][i] != 0) {
// the unicode of white circle (there is a player2 disc)
System.out.print('\u25CB');
}
else {
if (a1[j][i] != 0) {
// the unicode of black circle (there is a player1 disc)
System.out.print('\u25CF');
}
else
// the unicode of white square (a blank)
System.out.print('\u25A1');
}
System.out.print(" ");
}
System.out.println();
}
}
/**
* this method gets the coordinate of a blank space an checks if there is
* a opponent disc,and then if exists for each of opponent disks does the equalize method
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment