I am new to learning Android and am learning from Udemy.
The lecture which I am currently learning is not explained clearly and I would like some help. In this lecture, the instructor is teaching to create a game called connect 3 which is similar to Tic Tac Toe. I have multiple issues with this lecture.
Issue 1: To initialize two players, the instructor first made an integer with value o at the start. The code is
int activePlayer = 0
The logic for the player was written as :
if (activePlayer == 0) {
// change background image as red(This is not the real code);
activePlayer = 1;
}
I am unable to understand, if the integer was taken initially at a fixed value, how can it be changed to another without any calculation?
Issue 2: Next the instructor is showing us a way of managing the game state, ie initially clicking two times would change the connect 3 boxes from yellow to red and vice versa. The instructor stops this by taking an array of integers:
int [] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
After creating the arrays, the instructor assigned tags (android: tag) to all the 9 imageViews used in the game layout. The tag was converted to a string and assigned an integer value. The tags were named as int tappedCounter .
This is where it gets weird:
The instructor then wrote an if command comparing the tag with gameState. The code is :
if (gameState[tappedCounter] == 2 {
gameState[tappedCounter] = activePlayer;
if (activePlayer == 0) {
// change background image as red(This is not the real code);
activePlayer = 1;
} else {
//set backgroung image to blue;
activePlayer = 0;
}
Can anyone explain these things a little bit more clearly?
I am unable to understand, if the integer was taken initially at a fixed value, how can it be changed to another without any calculation.
You can assign different values to int variables, regardless if it is a result of calculation or constant.
It seems like the code
if(activePlayer==0){
activePlayer = 1;}
just switches the turn, ie if current turn was done by player 1 then switch to player 2, if turn was done by player 2 - switch to player one.
if( gameState[tappedCounter] == 2){
gameState[tappedCounter] = activePlayer;
if(activePlayer==0){
activePlayer = 1;}
else{
activePlayer =0;
}
}
The array of states describes state of each cell in the game. Seems like 2 - is state of empty cell. 0 - state of cell marked by player one ; 1 - state of cell marked of player 2. So this code does following - on each selection of cell:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.