Commit b95e5b9d authored by 9731301's avatar 9731301

Add calculations the winner Othello class

parent afc1cdc2
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="7be08370-19a4-4528-ab9b-d4cb0ec736c4" name="Default Changelist" comment=""> <list default="true" id="7be08370-19a4-4528-ab9b-d4cb0ec736c4" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/MyOthello/Othello.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/MyOthello/Othello.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/MyOthello/User.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/MyOthello/User.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Othello.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Othello.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/User.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/User.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/User.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/User.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
...@@ -60,7 +64,9 @@ ...@@ -60,7 +64,9 @@
<workItem from="1585656097487" duration="2378000" /> <workItem from="1585656097487" duration="2378000" />
<workItem from="1585659438353" duration="9321000" /> <workItem from="1585659438353" duration="9321000" />
<workItem from="1585687211713" duration="16102000" /> <workItem from="1585687211713" duration="16102000" />
<workItem from="1585733466673" duration="3956000" /> <workItem from="1585733466673" duration="4235000" />
<workItem from="1585743679588" duration="17656000" />
<workItem from="1585769658377" duration="6434000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -68,45 +74,49 @@ ...@@ -68,45 +74,49 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="574" y="97" key="#com.intellij.ide.util.MemberChooser" timestamp="1585736657332"> <state x="574" y="97" key="#com.intellij.ide.util.MemberChooser" timestamp="1585772436513">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="574" y="97" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1585736657332" /> <state x="574" y="97" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1585772436513" />
<state width="1493" height="250" key="GridCell.Tab.0.bottom" timestamp="1585738348489"> <state width="1493" height="63" key="GridCell.Tab.0.bottom" timestamp="1585776104986">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="250" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1585738348489" /> <state width="1493" height="63" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1585776104986" />
<state width="1493" height="250" key="GridCell.Tab.0.center" timestamp="1585738348488"> <state width="1493" height="63" key="GridCell.Tab.0.center" timestamp="1585776104985">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="250" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1585738348488" /> <state width="1493" height="63" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1585776104985" />
<state width="1493" height="250" key="GridCell.Tab.0.left" timestamp="1585738348488"> <state width="1493" height="63" key="GridCell.Tab.0.left" timestamp="1585776104985">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="250" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1585738348488" /> <state width="1493" height="63" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1585776104985" />
<state width="1493" height="250" key="GridCell.Tab.0.right" timestamp="1585738348488"> <state width="1493" height="63" key="GridCell.Tab.0.right" timestamp="1585776104985">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="250" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1585738348488" /> <state width="1493" height="63" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1585776104985" />
<state width="1493" height="182" key="GridCell.Tab.1.bottom" timestamp="1585671521268"> <state width="1493" height="331" key="GridCell.Tab.1.bottom" timestamp="1585762030361">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="182" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1585671521268" /> <state width="1493" height="331" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1585762030361" />
<state width="1493" height="182" key="GridCell.Tab.1.center" timestamp="1585671521268"> <state width="1493" height="331" key="GridCell.Tab.1.center" timestamp="1585762030361">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="182" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1585671521268" /> <state width="1493" height="331" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1585762030361" />
<state width="1493" height="182" key="GridCell.Tab.1.left" timestamp="1585671521267"> <state width="1493" height="331" key="GridCell.Tab.1.left" timestamp="1585762030361">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="182" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1585671521267" /> <state width="1493" height="331" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1585762030361" />
<state width="1493" height="182" key="GridCell.Tab.1.right" timestamp="1585671521268"> <state width="1493" height="331" key="GridCell.Tab.1.right" timestamp="1585762030361">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="182" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1585671521268" /> <state width="1493" height="331" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1585762030361" />
<state x="813" y="283" key="com.intellij.ide.util.TipDialog" timestamp="1585733471645"> <state x="813" y="283" key="com.intellij.ide.util.TipDialog" timestamp="1585769711025">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="813" y="283" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1585733471645" /> <state x="813" y="283" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1585769711025" />
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1585744923229">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1585744923229" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -8,6 +8,7 @@ public class Othello { ...@@ -8,6 +8,7 @@ public class Othello {
ArrayList <Block> blocks = new ArrayList<Block>(); ArrayList <Block> blocks = new ArrayList<Block>();
User user1 = new User(); User user1 = new User();
User user2 = new User(); User user2 = new User();
User chosenUser = new User();
Character c = 'n'; Character c = 'n';
int numberOfEmptyBlocksAtTheEnd = 0; int numberOfEmptyBlocksAtTheEnd = 0;
int row; int row;
...@@ -35,11 +36,11 @@ public class Othello { ...@@ -35,11 +36,11 @@ public class Othello {
user1.setName(sc.nextLine().trim()); user1.setName(sc.nextLine().trim());
while (!(c.equals('b') || c.equals('w'))) { while (!(c.equals('b') || c.equals('w'))) {
System.out.println("plz enter user1 color w or b"); System.out.println("plz enter user1 color w or b");
c = sc.next().charAt(0); c = sc.nextLine().charAt(0);
user1.setColor(c); user1.setColor(c);
} }
System.out.println("plz enter user2 name"); System.out.println("plz enter user2 name");
user2.setName(sc.next()); user2.setName(sc.nextLine().trim());
if (user1.getColor().equals('w')) if (user1.getColor().equals('w'))
user2.setColor('b'); user2.setColor('b');
else else
...@@ -64,13 +65,28 @@ public class Othello { ...@@ -64,13 +65,28 @@ public class Othello {
while (true) { while (true) {
System.out.println("plz enter your choice for example :3 a");
chosenBlock.changeColor(); if (chosenUser.equals(user1)){
System.out.println("turn is for : " + chosenBlock.getColor()); chosenUser = user2;
}
else {
chosenUser = user1;
}
System.out.println("turn is for : " + chosenUser.getName()+" that color is:"+chosenUser.getColor()+" plz enter your choice for example: 3 a");
String input = sc.nextLine().trim(); String input = sc.nextLine().trim();
String[] inputArray = input.split(" "); String[] inputArray = input.split(" ");
System.out.println(inputArray[0]);
row = Integer.parseInt(inputArray[0])-1; row = Integer.parseInt(inputArray[0])-1;
if (row > 8 || row <= 0){
System.out.println(" enter again correctly");
if (chosenUser.equals(user1)){
chosenUser = user2;
}
else {
chosenUser = user1;
}
continue;
}
if (inputArray[1].toLowerCase().equals("a")) if (inputArray[1].toLowerCase().equals("a"))
column = 0; column = 0;
else if (inputArray[1].toLowerCase().equals("b")) else if (inputArray[1].toLowerCase().equals("b"))
...@@ -88,39 +104,317 @@ public class Othello { ...@@ -88,39 +104,317 @@ public class Othello {
else if (inputArray[1].toLowerCase().equals("h")) else if (inputArray[1].toLowerCase().equals("h"))
column = 7; column = 7;
else { else {
System.out.println("enter again correctly"); System.out.println(" enter again correctly");
break; if (chosenUser.equals(user1)){
chosenUser = user2;
}
else {
chosenUser = user1;
}
continue;
} }
chosenBlock.setRow(row); chosenBlock.setRow(row);
chosenBlock.setColumn(column); chosenBlock.setColumn(column);
int flag = 0;
int flag1 = 0;// to check if neighbor has same color stop checking
int flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
int flag3 = 0;
for (Block b : blocks){
if (b.getColumn() == column && b.getRow() == row){
if ((b.getColor().equals('b') || b.getColor().equals('w') )){
flag = 1;
}
else {
//check down
for (int i = row + 1; i < 8; i++) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() && block1.getRow() == chosenBlock.getRow() + 1 && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;// flag1 is to stop loop if down neighbor has same color
break;
}
if (flag2 == 1) {// to not change " w b b b w b b w " to "w w w w w w w w"
break;
}
if (!block1.getColor().equals('n') && block1.getRow() == i && block1.getColumn() == column && block1.getColor().equals(chosenUser.getColor())) {
for (int rowIndex = row; rowIndex <= block1.getRow(); rowIndex++) {
for (Block block2 : blocks) {
if (block2.getColumn() == column && block2.getRow() == rowIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check up
flag1 = 0;//flag1 is to stop loop if up neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = row - 1; i >= 0; i--) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getRow() == chosenBlock.getRow() - 1 && block1.getColumn() == chosenBlock.getColumn() && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getRow() == i && block1.getColumn() == column && block1.getColor().equals(chosenUser.getColor())) {
for (int rowIndex = row; rowIndex >= block1.getRow(); rowIndex--) {
for (Block block2 : blocks) {
if (block2.getColumn() == column && block2.getRow() == rowIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check right
flag1 = 0;// flag1 is to stop loop if right neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column + 1; i < 8; i++) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() + 1 && block1.getRow() == chosenBlock.getRow() && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == row && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column; columnIndex <= block1.getRow(); columnIndex++) {
for (Block block2 : blocks) {
if (block2.getRow() == row && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check left
flag1 = 0;// flag1 is to stop loop if left neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column - 1; i >= 0; i--) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() - 1 && block1.getRow() == chosenBlock.getRow() && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == row && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column; columnIndex >= block1.getRow(); columnIndex--) {
for (Block block2 : blocks) {
if (block2.getRow() == row && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check down and right
flag1 = 0;// flag1 is to stop loop if down_right neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column + 1, j = row + 1; i < 8 && j < 8; i++, j++) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() + 1 && block1.getRow() == chosenBlock.getRow() + 1 && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == j && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column, rowIndex = row; rowIndex <= block1.getRow() && rowIndex <= block1.getRow(); columnIndex++, rowIndex++) {
for (Block block2 : blocks) {
if (block2.getRow() == rowIndex && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1) {
break;
}
}
//check down and left
flag1 = 0;// flag1 is to stop loop if down_left neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column - 1, j = row + 1; i > -1 && j < 8; i--, j++) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() - 1 && block1.getRow() == chosenBlock.getRow() + 1 && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == j && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column, rowIndex = row; rowIndex >= block1.getColumn() && rowIndex <= block1.getRow(); columnIndex--, rowIndex++) {
for (Block block2 : blocks) {
if (block2.getRow() == rowIndex && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check up and right
flag1 = 0;// flag1 is to stop loop if up_right neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column + 1, j = row - 1; i < 8 && j > -1; i++, j--) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() + 1 && block1.getRow() == chosenBlock.getRow() - 1 && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == j && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column, rowIndex = row; columnIndex <= block1.getColumn() && rowIndex >= block1.getRow(); columnIndex++, rowIndex--) {
for (Block block2 : blocks) {
if (block2.getRow() == rowIndex && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
//check up and left
flag1 = 0;// flag1 is to stop loop if up_left neighbor has same color
flag2 = 0;// to not change " w b b b w b b w " to "w w w w w w w w"
for (int i = column - 1, j = row - 1; i > -1 && j > -1; i--, j--) {// find the next block with chosen block color
for (Block block1 : blocks) {
if (block1.getColumn() == chosenBlock.getColumn() - 1 && block1.getRow() == chosenBlock.getRow() - 1 && (block1.getColor().equals(chosenUser.getColor()) || block1.getColor().equals('n'))) {
flag1 = 1;
break;
}
if (flag2 == 1) {
break;
}
if (!block1.getColor().equals('n') && block1.getColumn() == i && block1.getRow() == j && block1.getColor().equals(chosenUser.getColor())) {
for (int columnIndex = column, rowIndex = row; columnIndex >= block1.getColumn() && rowIndex >= block1.getRow(); columnIndex--, rowIndex--) {
for (Block block2 : blocks) {
if (block2.getRow() == rowIndex && block2.getColumn() == columnIndex) {//change color of blocks in 2 blocks with same color
block2.setColor(chosenUser.getColor());
flag2 = 1;
flag3 = 1;
}
}
}
}
}
if (flag1 == 1)
break;
}
}
}
}
if (flag3 == 0){
System.out.println("you entered wrong block try again");
if (chosenUser.equals(user1)){
chosenUser = user2;
}
else {
chosenUser = user1;
}
}
if (flag == 1){
System.out.println("block is not empty try again");
if (chosenUser.equals(user1)){
chosenUser = user2;
}
else {
chosenUser = user1;
}
}
else {
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 = 0 ; i < 8 ; i++){ for (int i = 0; i < 8; i++) {
System.out.print(i+1+" "); System.out.print(i + 1 + " ");
for (int j = 0 ; j < 8 ; j++){ for (int j = 0; j < 8; j++) {
for (Block b : blocks){ for (Block b : blocks) {
if (b.getRow() == i && b.getColumn() == j){ if (b.getRow() == i && b.getColumn() == j) {
System.out.print(b.getColor()+" "); System.out.print(b.getColor() + " ");
} }
} }
} }
System.out.println(); System.out.println();
} }
}
for (Block b : blocks){ for (Block b : blocks){
if (b.getColor().equals(user1.getColor())){ if (b.getColor().equals(user1.getColor())){
user1.addBlock(b); user1.addBlock(b);
user2.removeBlock(b);
} }
else if (b.getColor().equals(user2.getColor())){ else if (b.getColor().equals(user2.getColor())){
user2.addBlock(b); user2.addBlock(b);
user1.removeBlock(b);
} }
else { else {
user1.removeBlock(b);
user2.removeBlock(b);
numberOfEmptyBlocksAtTheEnd ++; numberOfEmptyBlocksAtTheEnd ++;
} }
System.out.println(user1.getName() + " = " + user1.getUserBlocks().size()+"_______" + user2.getName() + " = " +user2.getUserBlocks().size());
if (user1.isAvailableBlockToPick(b,blocks)){ if (user1.isAvailableBlockToPick(b,blocks)){
user1.addAvailableBlockToPick(b); user1.addAvailableBlockToPick(b);
} }
...@@ -134,6 +428,27 @@ public class Othello { ...@@ -134,6 +428,27 @@ public class Othello {
user2.removeAvailableBlockToPick(b); user2.removeAvailableBlockToPick(b);
} }
} }
System.out.println(user1.getName() + " = " + user1.getUserBlocks().size()+"_______" + user2.getName() + " = " +user2.getUserBlocks().size());
if (chosenUser.getAvailableBlocksToPick().size() == 0) {
System.out.println("pass");
continue;
}
if (user1.getAvailableBlocksToPick().size() == 0 && user2.getAvailableBlocksToPick().size() == 0){
if (user1.getUserBlocks() == user2.getUserBlocks()){
System.out.println("equal "+user1.getUserBlocks());
break;
}
else if (user1.getUserBlocks().size() > user2.getUserBlocks().size()){
System.out.println(user1.getName() + " = " + user1.getUserBlocks().size()+numberOfEmptyBlocksAtTheEnd +"_______" +user2.getName() + " = " +user2.getUserBlocks().size());
System.out.println(user1.getName() + " won");
break;
}
else {
System.out.println(user1.getName() + " = " + user1.getUserBlocks().size() + "_______"+user2.getName() + " = " +user2.getUserBlocks().size() +numberOfEmptyBlocksAtTheEnd);
System.out.println(user2.getName()+" won");
break;
}
}
} }
} }
......
...@@ -14,9 +14,13 @@ public class User { ...@@ -14,9 +14,13 @@ public class User {
userBlocks.add(block); userBlocks.add(block);
} }
public void removeBlock(Block block){
userBlocks.remove(block);
}
public boolean isAvailableBlockToPick(Block block , ArrayList<Block> blocks ) { public boolean isAvailableBlockToPick(Block block , ArrayList<Block> blocks ) {
int i, j; int i, j;
int flag1 = 0; int flag1 = 0;// for checking neighbors has same color in each step or not
int flag2 = 0; int flag2 = 0;
int flag3 = 0;// to know you can pick an empty block with help of checking changing color int flag3 = 0;// to know you can pick an empty block with help of checking changing color
if (!block.getColor().equals('n')) { if (!block.getColor().equals('n')) {
...@@ -26,7 +30,7 @@ public class User { ...@@ -26,7 +30,7 @@ public class User {
//check down //check down
for (i = block.getRow(); i > 8; i++) {// find the next block with chosen block color for (i = block.getRow(); i > 8; i++) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getRow() == block.getRow() + 1 && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() && block1.getRow() == block.getRow() + 1 && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -46,9 +50,9 @@ public class User { ...@@ -46,9 +50,9 @@ public class User {
//check up //check up
flag1 = 0; flag1 = 0;
flag2 = 0; flag2 = 0;
for (i = block.getRow() - 1; i >= 0; i--) {// find the next block with chosen block color for (i = block.getRow() - 1; i > -1; i--) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getRow() == block.getRow() - 1 && block1.getColumn() == block.getColumn() && block1.getColor().equals(color)) { if (block1.getRow() == block.getRow() - 1 && block1.getColumn() == block.getColumn() && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -70,7 +74,7 @@ public class User { ...@@ -70,7 +74,7 @@ public class User {
flag2 = 0; flag2 = 0;
for (i = block.getColumn() + 1; i < 8; i++) {// find the next block with chosen block color for (i = block.getColumn() + 1; i < 8; i++) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -90,9 +94,9 @@ public class User { ...@@ -90,9 +94,9 @@ public class User {
//check left //check left
flag1 = 0; flag1 = 0;
flag2 = 0; flag2 = 0;
for (i = block.getColumn() - 1; i >= 0; i--) {// find the next block with chosen block color for (i = block.getColumn() - 1; i > -1; i--) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() &&( block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -115,7 +119,7 @@ public class User { ...@@ -115,7 +119,7 @@ public class User {
flag2 = 0; flag2 = 0;
for (i = block.getColumn() + 1, j = block.getRow() - 1; i < 8 && j > -1; i++, j--) {// find the next block with chosen block color for (i = block.getColumn() + 1, j = block.getRow() - 1; i < 8 && j > -1; i++, j--) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() + 1 && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() + 1 && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -137,7 +141,7 @@ public class User { ...@@ -137,7 +141,7 @@ public class User {
flag2 = 0; flag2 = 0;
for (i = block.getColumn() - 1, j = block.getRow() + 1; i > -1 && j < 8; i--, j++) {// find the next block with chosen block color for (i = block.getColumn() - 1, j = block.getRow() + 1; i > -1 && j < 8; i--, j++) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() + 1 && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() + 1 && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -159,7 +163,7 @@ public class User { ...@@ -159,7 +163,7 @@ public class User {
flag2 = 0; flag2 = 0;
for (i = block.getColumn() + 1, j = block.getRow() + 1; i < 8 && j < 8; i++, j++) {// find the next block with chosen block color for (i = block.getColumn() + 1, j = block.getRow() + 1; i < 8 && j < 8; i++, j++) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() - 1 && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() + 1 && block1.getRow() == block.getRow() - 1 && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -179,9 +183,9 @@ public class User { ...@@ -179,9 +183,9 @@ public class User {
//check up and left //check up and left
flag1 = 0; flag1 = 0;
flag2 = 0; flag2 = 0;
for (i = block.getColumn() - 1, j = block.getRow() - 1; i < -1 && j < -1; i--, j--) {// find the next block with chosen block color for (i = block.getColumn() - 1, j = block.getRow() - 1; i > -1 && j > -1; i--, j--) {// find the next block with chosen block color
for (Block block1 : blocks) { for (Block block1 : blocks) {
if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() - 1 && block1.getColor().equals(color)) { if (block1.getColumn() == block.getColumn() - 1 && block1.getRow() == block.getRow() - 1 && (block1.getColor().equals(color) || block1.getColor().equals('n'))) {
flag1 = 1; flag1 = 1;
break; break;
} }
...@@ -238,5 +242,20 @@ public class User { ...@@ -238,5 +242,20 @@ public class User {
this.name = name; this.name = name;
} }
public ArrayList<Block> getAvailableBlocksToPick() {
return availableBlocksToPick;
}
public void setAvailableBlocksToPick(ArrayList<Block> availableBlocksToPick) {
this.availableBlocksToPick = availableBlocksToPick;
}
public ArrayList<Block> getUserBlocks() {
return userBlocks;
}
public void setUserBlocks(ArrayList<Block> userBlocks) {
this.userBlocks = userBlocks;
}
} }
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