Commit 04b7356e authored by 9731301's avatar 9731301

add a method to User class to choose a winner

parent 17fe9228
......@@ -3,6 +3,8 @@
<component name="ChangeListManager">
<list default="true" id="63729159-695b-4499-ba7f-f31e81a0234c" 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/MyPentago/Block.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/MyPentago/Block.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/MyPentago/Pentago.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/MyPentago/Pentago.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -58,7 +60,10 @@
<workItem from="1586099364327" duration="87000" />
<workItem from="1586105455800" duration="97000" />
<workItem from="1586105975356" duration="8819000" />
<workItem from="1586162373379" duration="6300000" />
<workItem from="1586162373379" duration="6530000" />
<workItem from="1586174700430" duration="9185000" />
<workItem from="1586246845885" duration="2399000" />
<workItem from="1586261785897" duration="13320000" />
</task>
<servers />
</component>
......@@ -66,45 +71,63 @@
<option name="version" value="1" />
</component>
<component name="WindowStateProjectService">
<state width="1493" height="2" key="GridCell.Tab.0.bottom" timestamp="1586168477737">
<state width="1493" height="564" key="GridCell.Tab.0.bottom" timestamp="1586275386382">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1586168477737" />
<state width="1493" height="2" key="GridCell.Tab.0.center" timestamp="1586168477737">
<state width="1493" height="564" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1586275386382" />
<state width="1493" height="564" key="GridCell.Tab.0.center" timestamp="1586275386382">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1586168477737" />
<state width="1493" height="2" key="GridCell.Tab.0.left" timestamp="1586168477737">
<state width="1493" height="564" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1586275386382" />
<state width="1493" height="564" key="GridCell.Tab.0.left" timestamp="1586275386382">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1586168477737" />
<state width="1493" height="2" key="GridCell.Tab.0.right" timestamp="1586168477737">
<state width="1493" height="564" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1586275386382" />
<state width="1493" height="564" key="GridCell.Tab.0.right" timestamp="1586275386382">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1586168477737" />
<state width="1493" height="358" key="GridCell.Tab.1.bottom" timestamp="1586168209278">
<state width="1493" height="564" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1586275386382" />
<state width="1493" height="194" key="GridCell.Tab.1.bottom" timestamp="1586274892633">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="358" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1586168209278" />
<state width="1493" height="358" key="GridCell.Tab.1.center" timestamp="1586168209278">
<state width="1493" height="194" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1586274892633" />
<state width="1493" height="194" key="GridCell.Tab.1.center" timestamp="1586274892633">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="358" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1586168209278" />
<state width="1493" height="358" key="GridCell.Tab.1.left" timestamp="1586168209278">
<state width="1493" height="194" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1586274892633" />
<state width="1493" height="194" key="GridCell.Tab.1.left" timestamp="1586274892633">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="358" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1586168209278" />
<state width="1493" height="358" key="GridCell.Tab.1.right" timestamp="1586168209278">
<state width="1493" height="194" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1586274892633" />
<state width="1493" height="194" key="GridCell.Tab.1.right" timestamp="1586274892633">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="358" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1586168209278" />
<state x="281" y="271" key="com.intellij.ide.util.TipDialog" timestamp="1586162378517">
<state width="1493" height="194" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1586274892633" />
<state x="281" y="271" key="com.intellij.ide.util.TipDialog" timestamp="1586261829792">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="281" y="271" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1586162378517" />
<state x="281" y="271" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1586261829792" />
<state x="25" y="67" key="new project wizard" timestamp="1586105483065">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="25" y="67" key="new project wizard/0.0.1536.824@0.0.1536.824" timestamp="1586105483065" />
<state x="425" y="187" key="run.anything.popup" timestamp="1586180125854">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="425" y="187" key="run.anything.popup/0.0.1536.824@0.0.1536.824" timestamp="1586180125854" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-method">
<url>file://$PROJECT_DIR$/src/User.java</url>
<line>27</line>
<properties class="User" method="isWinner">
<option name="EMULATED" value="true" />
</properties>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>
\ No newline at end of file
......@@ -41,7 +41,7 @@ public class Block {
allCells.remove(6);
allCells.add(8,allCells.get(0));
allCells.remove(8);
allCells.remove(0);
allCells.add(0,allCells.get(8));
allCells.remove(9);
......
......@@ -10,10 +10,12 @@ public class Pentago {
User chosenUser = new User();
chosenUser = user2;
user1.setColor('⚫');
user2.setColor('m');
user2.setColor('0');
int blockNumber;
int cellNumber;
int i , j ;
int blockNumberToTurn;
int i ;
boolean ans = false;
String turn;
for ( i = 0 ; i < 4 ; i++ ){
Block block = new Block();
......@@ -21,59 +23,54 @@ public class Pentago {
block.setNumber(i);
blocks.add(block);
}
printBlocks(blocks);
j = 0;
for ( i = 0 ; i < 4 ;){
System.out.print("| " + blocks.get(i).getAllCells().get(j++).getColor()+ " "+ blocks.get(i).getAllCells().get(j++).getColor() +" "+ blocks.get(i).getAllCells().get(j++).getColor() + " |");
if ( i == 3 && j == 9){
while (true) {
if (chosenUser.isWinner(blocks)){
System.out.println(chosenUser.getColor() + " is winner");
break;
}
if ((i == 0 || i == 2) && j == 3 ){
i++;
j = 0;
}
else if ((i == 0 || i == 2 ) && j == 6){
i++;
j = 3;
}
else if(( i == 0 || i == 2 ) && j == 9){
i++;
j = 6;
}
else if (( i == 1 )&& j == 9){
i = 2 ;
j = 0 ;
System.out.println("\n------------------");
continue;
}
else if ( (j == 3 || j == 6 ) && ( i == 1 || i == 3)){
i--;
System.out.println();
}
}
while (true) {
//pass the turn if there is not any cell to choose for player
if (chosenUser.equals(user1)){
chosenUser = user2;
}
else {
chosenUser = user1;
}
//if there is not any cell to choose foe chosen user
for (Block b : blocks){
for (int k = 0 ; k < 9 ; k++) {
if (b.getAllCells().get(k).getColor().equals('O')) {
ans = true;
break;
}
}
}
if ( ans == false){
System.out.println("pass");
continue;
}
System.out.println("\nplz enter a block number");
blockNumber = scanner.nextInt()-1;
blockNumber = scanner.nextInt() - 1 ;
System.out.println("plz enter a cell number");
cellNumber = scanner.nextInt()-1;
cellNumber = scanner.nextInt() - 1 ;
if (chosenUser.cellIsAvailableToTake(blocks.get(blockNumber).getAllCells().get(cellNumber))) {
blocks.get(blockNumber).getAllCells().get(cellNumber).setColor(chosenUser.getColor());
System.out.println(blocks.get(blockNumber).getAllCells().get(cellNumber).getColor());
}
// System.out.println("plz enter how change position? right/left");
// turn = scanner.next().trim();
// blocks.get(blockNumber).changePositionOfBlock(turn);
j = 0 ;
i = 0;
System.out.println(blocks.get(i).getAllCells().get(j++).getColor()+ " "+ blocks.get(i).getAllCells().get(j ++).getColor());
printBlocks(blocks);
System.out.println();
System.out.println("plz enter the number of block you wanna turn");
blockNumberToTurn =scanner.nextInt() - 1 ;
System.out.println("plz enter how change position? right/left");
turn = scanner.next().trim();
blocks.get(blockNumberToTurn).changePositionOfBlock(turn);
printBlocks(blocks);
}
}
public static void printBlocks( ArrayList<Block> blocks){
int i , j;
j = 0;
for ( i = 0 ; i < 4 ;){
System.out.print("| " + blocks.get(i).getAllCells().get(j++).getColor()+ " "+ blocks.get(i).getAllCells().get(j++).getColor() +" "+ blocks.get(i).getAllCells().get(j++).getColor() + " |");
......@@ -104,5 +101,4 @@ public class Pentago {
}
}
}
}
}
......@@ -24,4 +24,112 @@ public class User {
public void setColor(Character color) {
this.color = color;
}
public boolean isWinner(ArrayList<Block> blocks){
//choose the winner
int counter = 0 ;
for( int cellIndex = 0 , blockIndex = 0; counter < 12 ; ){// checking horizental
if ( blocks.get(blockIndex).getAllCells().get(cellIndex).getColor().equals(color)
&& blocks.get(blockIndex).getAllCells().get(cellIndex + 1).getColor().equals(color)
&& blocks.get(blockIndex).getAllCells().get(cellIndex + 2).getColor().equals(color)
&& blocks.get(blockIndex + 1).getAllCells().get(cellIndex ).getColor().equals(color)
&& blocks.get(blockIndex + 1).getAllCells().get(cellIndex + 1).getColor().equals(color)){
return true;
}
counter ++ ;
if (counter == 3 || counter == 9){
blockIndex = 2;
}
if ( counter == 6){
cellIndex = 1 ;
blockIndex = 0 ;
}
}
counter = 0 ;
for( int cellIndex = 0 , blockIndex = 0; counter < 12 ; ){// checking vertical
if ( blocks.get(blockIndex).getAllCells().get(cellIndex).getColor().equals(color)
&& blocks.get(blockIndex).getAllCells().get(cellIndex + 3).getColor().equals(color)
&& blocks.get(blockIndex).getAllCells().get(cellIndex + 6).getColor().equals(color)
&& blocks.get(blockIndex + 2).getAllCells().get(cellIndex ).getColor().equals(color)
&& blocks.get(blockIndex + 2).getAllCells().get(cellIndex + 3).getColor().equals(color)){
return true;
}
counter ++ ;
if ( counter == 3 || counter == 9){
blockIndex = 1 ;
}
if (counter == 6){
blockIndex = 0 ;
cellIndex = 1 ;
}
}
//check diagonal
if ( blocks.get(1).getAllCells().get(1).equals(color)
&& blocks.get(1).getAllCells().get(3).getColor().equals(color)
&& blocks.get(2).getAllCells().get(1).getColor().equals(color)
&& blocks.get(2).getAllCells().get(3).getColor().equals(color)
&& blocks.get(0).getAllCells().get(8).getColor().equals(color)){
return true;
}
if ( blocks.get(1).getAllCells().get(2).getColor().equals(color)
&& blocks.get(1).getAllCells().get(4).getColor().equals(color)
&& blocks.get(1).getAllCells().get(6).getColor().equals(color)
&& blocks.get(2).getAllCells().get(2).getColor().equals(color)
&& blocks.get(2).getAllCells().get(4).getColor().equals(color)){
return true;
}
if ( blocks.get(1).getAllCells().get(4).getColor().equals(color)
&& blocks.get(1).getAllCells().get(6).getColor().equals(color)
&& blocks.get(2).getAllCells().get(2).getColor().equals(color)
&& blocks.get(2).getAllCells().get(4).getColor().equals(color)
&& blocks.get(2).getAllCells().get(6).getColor().equals(color)){
return true;
}
if ( blocks.get(1).getAllCells().get(5).getColor().equals(color)
&& blocks.get(1).getAllCells().get(7).getColor().equals(color)
&& blocks.get(3).getAllCells().get(5).getColor().equals(color)
&& blocks.get(3).getAllCells().get(7).getColor().equals(color)
&& blocks.get(3).getAllCells().get(0).getColor().equals(color)){
return true;
}
if ( blocks.get(0).getAllCells().get(0).equals(color)
&& blocks.get(0).getAllCells().get(4).getColor().equals(color)
&& blocks.get(0).getAllCells().get(8).getColor().equals(color)
&& blocks.get(3).getAllCells().get(0).getColor().equals(color)
&& blocks.get(3).getAllCells().get(4).getColor().equals(color)){
return true;
}
if ( blocks.get(0).getAllCells().get(1).getColor().equals(color)
&& blocks.get(0).getAllCells().get(5).getColor().equals(color)
&& blocks.get(1).getAllCells().get(6).getColor().equals(color)
&& blocks.get(3).getAllCells().get(1).getColor().equals(color)
&& blocks.get(3).getAllCells().get(5).getColor().equals(color)){
return true;
}
if ( blocks.get(0).getAllCells().get(3).getColor().equals(color)
&& blocks.get(0).getAllCells().get(7).getColor().equals(color)
&& blocks.get(2).getAllCells().get(2).getColor().equals(color)
&& blocks.get(3).getAllCells().get(3).getColor().equals(color)
&& blocks.get(3).getAllCells().get(7).getColor().equals(color)){
return true;
}
if ( blocks.get(0).getAllCells().get(4).getColor().equals(color)
&& blocks.get(0).getAllCells().get(8).getColor().equals(color)
&& blocks.get(3).getAllCells().get(0).getColor().equals(color)
&& blocks.get(3).getAllCells().get(4).getColor().equals(color)
&& blocks.get(3).getAllCells().get(8).getColor().equals(color)){
return true;
}
return false;
}
}
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