Commit 5ad9f2e8 authored by amir's avatar amir

Scanning in graphic version

parent 5a06e374
package com.company; package com.company;
import java.util.Scanner;
public class Console extends Display { public class Console extends Display {
public Console(byte displayType) { public Console(byte displayType) {
super(displayType); super(displayType);
} }
@Override
public void display(int[][] a1, int[][] a2) { public void display(int[][] a1, int[][] a2) {
super.display(a1, a2); super.display(a1, a2);
System.out.println(" A B C D E F G H"); System.out.println(" A B C D E F G H");
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
System.out.print(i + " ");
for (int j = 1; j <= 8; j++) { for (int j = 1; j <= 8; j++) {
if (a2[j][i] != 0) { if (a2[j][i] != 0) {
// the unicode of white circle (there is a player2 disc) // the unicode of white circle (there is a player2 disc)
...@@ -30,4 +32,16 @@ public class Console extends Display { ...@@ -30,4 +32,16 @@ public class Console extends Display {
System.out.println(); 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 { ...@@ -14,4 +14,9 @@ public class Display {
public void display(int[][] a1, int[][] a2){ 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 { ...@@ -82,7 +82,10 @@ public class Graphic extends Display {
l1 = new JLabel(new ImageIcon("untitled.png")); l1 = new JLabel(new ImageIcon("untitled.png"));
f.setSize(700, 700); f.setSize(700, 700);
f.setLayout(null); f.setLayout(null);
f.setVisible(true); if (displayType == 0)
f.setVisible(false);
else
f.setVisible(true);
f.add(b); f.add(b);
b.setBounds(300,621,100, 40);//x axis, y axis, width, height b.setBounds(300,621,100, 40);//x axis, y axis, width, height
b.addActionListener(new ActionListener() { b.addActionListener(new ActionListener() {
...@@ -327,4 +330,22 @@ public class Graphic extends Display { ...@@ -327,4 +330,22 @@ public class Graphic extends Display {
} }
return null; 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; package com.company;
import java.util.Scanner; import java.util.Scanner;
public class Main { public class Main {
...@@ -11,15 +13,15 @@ public class Main { ...@@ -11,15 +13,15 @@ public class Main {
byte displayType = scanner.nextByte(); byte displayType = scanner.nextByte();
Player1 play1 = new Player1(); Player1 play1 = new Player1();
Player2 play2 = new Player2(); Player2 play2 = new Player2();
Console console = new Console(displayType);
Graphic graphic = new Graphic(displayType);
if (displayType == 0) { if (displayType == 0) {
Console console = new Console(displayType);
console.display(play1.getArr1(), play2.getArr2()); console.display(play1.getArr1(), play2.getArr2());
} }
else { else {
Graphic graphic = new Graphic(displayType);
graphic.display(play1.getArr1(), play2.getArr2()); 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)) (play1.getNumberOfDisks(play1.getArr1()) != 0) && (play2.getNumberOfDisks(play2.getArr2()) != 0))
{ {
if ((play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) == 0) && if ((play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) == 0) &&
...@@ -28,6 +30,7 @@ public class Main { ...@@ -28,6 +30,7 @@ public class Main {
turns++; turns++;
if (turns %2 == 1){ if (turns %2 == 1){
if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) { if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) {
if ()
System.out.println("Player1, it's your turn!"); System.out.println("Player1, it's your turn!");
} }
else { else {
...@@ -37,19 +40,26 @@ public class Main { ...@@ -37,19 +40,26 @@ public class Main {
} }
} }
if (turns %2 == 0){ if (turns %2 == 0){
if (play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) != 0) if (play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) != 0) {
System.out.println("Player2, it's your turn!"); //System.out.println("Player2, it's your turn!");
}
else { else {
//if there is no possible move prints pass //if there is no possible move prints pass
System.out.println("pass"); System.out.println("pass");
continue; continue;
} }
} }
String nl = scanner.nextLine(); int[] inputs = new int[2];
char[] f = nl.toCharArray(); if (displayType == 0) {
// scanning the x and y coordinates from user inputs = console.getInput();
int y = f[0] - '1' + 1; }
int x = f[2] - 'A' + 1; 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 ((x >= 1) && (x < 9) && (y >= 1) && (y < 9)) {
if ((!play1.containsInArray(play1.getArr1(), x, y)) && (!play2.containsInArray(play2.getArr2(), x, y))){ if ((!play1.containsInArray(play1.getArr1(), x, y)) && (!play2.containsInArray(play2.getArr2(), x, y))){
if (turns %2 == 1){ if (turns %2 == 1){
...@@ -71,7 +81,7 @@ public class Main { ...@@ -71,7 +81,7 @@ public class Main {
if (play1.getNumberOfDisks(play1.getArr1()) > play2.getNumberOfDisks(play2.getArr2())) if (play1.getNumberOfDisks(play1.getArr1()) > play2.getNumberOfDisks(play2.getArr2()))
System.out.println("Player1 wins!"); System.out.println("Player1 wins!");
else else
System.out.println("Player2 wins!");*/ System.out.println("Player2 wins!");
} }
......
...@@ -32,6 +32,35 @@ public class Players { ...@@ -32,6 +32,35 @@ public class Players {
return false; 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 * 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 * 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