Spin the Bottle Game Resources


About Game & Developer

Click Here

This All the Resources of Making the Spin the Bottle Game and Earn Money from them online.

Main Activity.Java File

Downlaod the text-file

  • package com.codinginflow.tictactoe; 
    1. import android.support.v7.app.AppCompatActivity;
    2. import android.os.Bundle;
    3. import android.view.View;
    4. import android.widget.Button;
    5. import android.widget.TextView;
    6. import android.widget.Toast;
    7.  
    8. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    9.  
    10.     private Button[][] buttons = new Button[3][3];
    11.  
    12.     private boolean player1Turn = true;
    13.  
    14.     private int roundCount;
    15.  
    16.     private int player1Points;
    17.     private int player2Points;
    18.  
    19.     private TextView textViewPlayer1;
    20.     private TextView textViewPlayer2;
    21.  
    22.     @Override
    23.     protected void onCreate(Bundle savedInstanceState) {
    24.         super.onCreate(savedInstanceState);
    25.         setContentView(R.layout.activity_main);
    26.  
    27.         textViewPlayer1 = findViewById(R.id.text_view_p1);
    28.         textViewPlayer2 = findViewById(R.id.text_view_p2);
    29.  
    30.         for (int i = 0; i < 3; i++) {
    31.             for (int j = 0; j < 3; j++) {
    32.                 String buttonID = "button_" + i + j;
    33.                 int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    34.                 buttons[i][j] = findViewById(resID);
    35.                 buttons[i][j].setOnClickListener(this);
    36.             }
    37.         }
    38.  
    39.         Button buttonReset = findViewById(R.id.button_reset);
    40.         buttonReset.setOnClickListener(new View.OnClickListener() {
    41.             @Override
    42.             public void onClick(View v) {
    43.                 resetGame();
    44.             }
    45.         });
    46.     }
    47.  
    48.     @Override
    49.     public void onClick(View v) {
    50.         if (!((Button) v).getText().toString().equals("")) {
    51.             return;
    52.         }
    53.  
    54.         if (player1Turn) {
    55.             ((Button) v).setText("X");
    56.         } else {
    57.             ((Button) v).setText("O");
    58.         }
    59.  
    60.         roundCount++;
    61.  
    62.         if (checkForWin()) {
    63.             if (player1Turn) {
    64.                 player1Wins();
    65.             } else {
    66.                 player2Wins();
    67.             }
    68.         } else if (roundCount == 9) {
    69.             draw();
    70.         } else {
    71.             player1Turn = !player1Turn;
    72.         }
    73.  
    74.     }
    75.  
    76.     private boolean checkForWin() {
    77.         String[][] field = new String[3][3];
    78.  
    79.         for (int i = 0; i < 3; i++) {
    80.             for (int j = 0; j < 3; j++) {
    81.                 field[i][j] = buttons[i][j].getText().toString();
    82.             }
    83.         }
    84.  
    85.         for (int i = 0; i < 3; i++) {
    86.             if (field[i][0].equals(field[i][1])
    87.                     && field[i][0].equals(field[i][2])
    88.                     && !field[i][0].equals("")) {
    89.                 return true;
    90.             }
    91.         }
    92.  
    93.         for (int i = 0; i < 3; i++) {
    94.             if (field[0][i].equals(field[1][i])
    95.                     && field[0][i].equals(field[2][i])
    96.                     && !field[0][i].equals("")) {
    97.                 return true;
    98.             }
    99.         }
    100.  
    101.         if (field[0][0].equals(field[1][1])
    102.                 && field[0][0].equals(field[2][2])
    103.                 && !field[0][0].equals("")) {
    104.             return true;
    105.         }
    106.  
    107.         if (field[0][2].equals(field[1][1])
    108.                 && field[0][2].equals(field[2][0])
    109.                 && !field[0][2].equals("")) {
    110.             return true;
    111.         }
    112.  
    113.         return false;
    114.     }
    115.  
    116.     private void player1Wins() {
    117.         player1Points++;
    118.         Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
    119.         updatePointsText();
    120.         resetBoard();
    121.     }
    122.  
    123.     private void player2Wins() {
    124.         player2Points++;
    125.         Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
    126.         updatePointsText();
    127.         resetBoard();
    128.     }
    129.  
    130.     private void draw() {
    131.         Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
    132.         resetBoard();
    133.     }
    134.  
    135.     private void updatePointsText() {
    136.         textViewPlayer1.setText("Player 1: " + player1Points);
    137.         textViewPlayer2.setText("Player 2: " + player2Points);
    138.     }
    139.  
    140.     private void resetBoard() {
    141.         for (int i = 0; i < 3; i++) {
    142.             for (int j = 0; j < 3; j++) {
    143.                 buttons[i][j].setText("");
    144.             }
    145.         }
    146.  
    147.         roundCount = 0;
    148.         player1Turn = true;
    149.     }
    150.  
    151.     private void resetGame() {
    152.         player1Points = 0;
    153.         player2Points = 0;
    154.         updatePointsText();
    155.         resetBoard();
    156.     }
    157.  
    158.     @Override
    159.     protected void onSaveInstanceState(Bundle outState) {
    160.         super.onSaveInstanceState(outState);
    161.  
    162.         outState.putInt("roundCount", roundCount);
    163.         outState.putInt("player1Points", player1Points);
    164.         outState.putInt("player2Points", player2Points);
    165.         outState.putBoolean("player1Turn", player1Turn);
    166.     }
    167.  
    168.     @Override
    169.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
    170.         super.onRestoreInstanceState(savedInstanceState);
    171.  
    172.         roundCount = savedInstanceState.getInt("roundCount");
    173.         player1Points = savedInstanceState.getInt("player1Points");
    174.         player2Points = savedInstanceState.getInt("player2Points");
    175.         player1Turn = savedInstanceState.getBoolean("player1Turn");
    176.     }
    177. }

    Download the Text-file

    Downlaod Jave file