diff --git a/Android/TicTacToy Game/TicTacToyLocal/StartUp/app/src/main/java/com/hussein/startup/MainActivity.kt b/Android/TicTacToy Game/TicTacToyLocal/StartUp/app/src/main/java/com/hussein/startup/MainActivity.kt index bf64ea8..c284f7e 100644 --- a/Android/TicTacToy Game/TicTacToyLocal/StartUp/app/src/main/java/com/hussein/startup/MainActivity.kt +++ b/Android/TicTacToy Game/TicTacToyLocal/StartUp/app/src/main/java/com/hussein/startup/MainActivity.kt @@ -1,3 +1,8 @@ +/* +Notes: add the game reset and game draw, also fix the draw error when list.size <= 0 and java error (game crash) in Random().nextInt(n) +when n <= 0 :0 that happens in the minute 5:10 on class 75. +*/ + package com.hussein.startup import android.graphics.Color @@ -15,8 +20,7 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } - - + protected fun buClick(view:View){ val buSelected= view as Button var cellID=0 @@ -30,19 +34,16 @@ class MainActivity : AppCompatActivity() { R.id.bu7-> cellID=7 R.id.bu8-> cellID=8 R.id.bu9-> cellID=9 - } // Toast.makeText(this,"ID:"+ cellID, Toast.LENGTH_LONG).show() - PlayGame(cellID,buSelected) } - + var player1=ArrayList() var player2=ArrayList() var ActivePlayer=1 - + fun PlayGame(cellID:Int,buSelected:Button){ - if(ActivePlayer==1){ buSelected.text="X" buSelected.setBackgroundResource(R.color.blue) @@ -55,17 +56,12 @@ class MainActivity : AppCompatActivity() { player2.add(cellID) ActivePlayer=1 } - - buSelected.isEnabled=false CheckWiner() } - - - + fun CheckWiner(){ var winer=-1 - // row 1 if(player1.contains(1) && player1.contains(2) && player1.contains(3)){ winer=1 @@ -74,7 +70,6 @@ class MainActivity : AppCompatActivity() { winer=2 } - // row 2 if(player1.contains(4) && player1.contains(5) && player1.contains(6)){ winer=1 @@ -82,10 +77,6 @@ class MainActivity : AppCompatActivity() { if(player2.contains(4) && player2.contains(5) && player2.contains(6)){ winer=2 } - - - - // row 3 if(player1.contains(7) && player1.contains(8) && player1.contains(9)){ winer=1 @@ -93,9 +84,6 @@ class MainActivity : AppCompatActivity() { if(player2.contains(7) && player2.contains(8) && player2.contains(9)){ winer=2 } - - - // col 1 if(player1.contains(1) && player1.contains(4) && player1.contains(7)){ winer=1 @@ -103,9 +91,6 @@ class MainActivity : AppCompatActivity() { if(player2.contains(1) && player2.contains(4) && player2.contains(7)){ winer=2 } - - - // col 2 if(player1.contains(2) && player1.contains(5) && player1.contains(8)){ winer=1 @@ -113,8 +98,6 @@ class MainActivity : AppCompatActivity() { if(player2.contains(2) && player2.contains(5) && player2.contains(8)){ winer=2 } - - // col 3 if(player1.contains(3) && player1.contains(6) && player1.contains(9)){ winer=1 @@ -122,24 +105,18 @@ class MainActivity : AppCompatActivity() { if(player2.contains(3) && player2.contains(6) && player2.contains(9)){ winer=2 } - - if( winer != -1){ - if (winer==1){ Toast.makeText(this," Player 1 win the game", Toast.LENGTH_LONG).show() + GameReset() }else{ Toast.makeText(this," Player 2 win the game", Toast.LENGTH_LONG).show() - + GameReset() } - } - } - - + fun AutoPlay(){ - var emptyCells=ArrayList() for ( cellID in 1..9){ @@ -147,31 +124,39 @@ class MainActivity : AppCompatActivity() { emptyCells.add(cellID) } } - - val r=Random() - val randIndex=r.nextInt(emptyCells.size-0)+0 - val cellID= emptyCells[randIndex] - - var buSelect:Button? - when(cellID){ - 1-> buSelect=bu1 - 2-> buSelect=bu2 - 3-> buSelect=bu3 - 4-> buSelect=bu4 - 5-> buSelect=bu5 - 6-> buSelect=bu6 - 7-> buSelect=bu7 - 8-> buSelect=bu8 - 9-> buSelect=bu9 - else->{ - buSelect=bu1 + //add this if to prevent game crash when the games draw + //also prevent java erro in next in when n <= 0 :0 + if(emptyCells.size > 0){ + val randIndex=r.nextInt(emptyCells.size-0)+0 + val cellID= emptyCells[randIndex] + var buSelect:Button? + when(cellID){ + 1-> buSelect=bu1 + 2-> buSelect=bu2 + 3-> buSelect=bu3 + 4-> buSelect=bu4 + 5-> buSelect=bu5 + 6-> buSelect=bu6 + 7-> buSelect=bu7 + 8-> buSelect=bu8 + 9-> buSelect=bu9 + else->{ + buSelect=bu1 + } } + PlayGame(cellID,buSelect) + }else { + //This is a game draw + Toast.makeText(this," Game Draw!", Toast.LENGTH_LONG).show() + GameReset() } - - PlayGame(cellID,buSelect) - } - - + + fun GameReset(){ + var emptyList=ArrayList() + this.player1=emptyList + this.player2=emptyList + this.ActivePlayer=1 + } }