Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Random().nextInt() error + add Game Draw #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<Int>()
var player2=ArrayList<Int>()
var ActivePlayer=1

fun PlayGame(cellID:Int,buSelected:Button){

if(ActivePlayer==1){
buSelected.text="X"
buSelected.setBackgroundResource(R.color.blue)
Expand All @@ -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
Expand All @@ -74,104 +70,93 @@ class MainActivity : AppCompatActivity() {
winer=2
}


// row 2
if(player1.contains(4) && player1.contains(5) && player1.contains(6)){
winer=1
}
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
}
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
}
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
}
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
}
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<Int>()
for ( cellID in 1..9){

if(!( player1.contains(cellID) || player2.contains(cellID))) {
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<Int>()
this.player1=emptyList
this.player2=emptyList
this.ActivePlayer=1
}
}