0% found this document useful (0 votes)
136 views

Tic Tac Toe Game Script

This document contains code for a Tic Tac Toe game app. It defines a MainActivity class that handles button clicks and game logic. It initializes a grid of buttons to represent the game board. It also defines a TicTacToeGame class to manage the game state, including tracking the board, current player, and handling moves, checking for wins/draws, and resetting.

Uploaded by

dort developers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Tic Tac Toe Game Script

This document contains code for a Tic Tac Toe game app. It defines a MainActivity class that handles button clicks and game logic. It initializes a grid of buttons to represent the game board. It also defines a TicTacToeGame class to manage the game state, including tracking the board, current player, and handling moves, checking for wins/draws, and resetting.

Uploaded by

dort developers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.example.

myfirstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

// EditText emailEditText;
// EditText passwordEditText;
// Button signInButton;
// TextView resultTextView;
//
//
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//
// emailEditText = findViewById(R.id.editTextTextEmailAddress);
// passwordEditText = findViewById(R.id.editTextTextPassword);
// signInButton = findViewById(R.id.SignIn);
// resultTextView = findViewById(R.id.textViewResult);
//
// signInButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// String email = emailEditText.getText().toString().trim();
// String password =
passwordEditText.getText().toString().trim();
//
// if (email.isEmpty() || password.isEmpty()) {
// Toast.makeText(MainActivity.this, "Please enter email
and password", Toast.LENGTH_SHORT).show();
// } else {
// resultTextView.setText("Email: " + email + "\nPassword:
" + password);
// resultTextView.setVisibility(View.VISIBLE);
// Handler handler = new Handler();
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// resultTextView.setVisibility(View.GONE);
// }
// }, 3000); // Display for 3 seconds (3000 milliseconds)
// }
//
// }
// });
// }

private TicTacToeGame game;


private Button[][] buttons;
private TextView statusTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

game = new TicTacToeGame();


buttons = new Button[3][3];
statusTextView = findViewById(R.id.statusTextView);

initializeButtons();

updateStatusText();
}

private void initializeButtons() {


buttons[0][0] = findViewById(R.id.button1);
buttons[0][1] = findViewById(R.id.button2);
buttons[0][2] = findViewById(R.id.button3);
buttons[1][0] = findViewById(R.id.button4);
buttons[1][1] = findViewById(R.id.button5);
buttons[1][2] = findViewById(R.id.button6);
buttons[2][0] = findViewById(R.id.button7);
buttons[2][1] = findViewById(R.id.button8);
buttons[2][2] = findViewById(R.id.button9);
// Initialize the rest of the buttons (button2, button3, ..., button9)
// buttons[0][1] = findViewById(R.id.button2);
// buttons[0][2] = findViewById(R.id.button3);
// ...
// buttons[2][2] = findViewById(R.id.button9)

for (int row = 0; row < 3; row++) {


for (int col = 0; col < 3; col++) {
buttons[row][col].setOnClickListener
(new View.OnClickListener()
{
@Override
public void onClick(View v) {

onButtonClick(v);
}
});
}
}
}

public void onButtonClick(View view) {


Button button = (Button) view;

int row = getButtonRow(button);


int col = getButtonCol(button);
if (game.makeMove(row, col)) {
button.setText(String.valueOf(game.getCurrentPlayer()));
button.setEnabled(false);

if (game.checkForWin()) {
statusTextView.setText("Player " + game.getCurrentPlayer() + "
wins!");
disableAllButtons();
} else if (game.isBoardFull()) {
statusTextView.setText("It's a draw!");
disableAllButtons();
} else {
game.switchPlayer();
updateStatusText();
}
}
}

public void onResetButtonClick(View view) {


game.reset();
enableAllButtons();
clearButtonText();
updateStatusText();
}

private int getButtonRow(Button button) {


// Get the row index of the button
// Example: button1 -> row = 0
// Example: button2 -> row = 0
// Example: button4 -> row = 1
// ...

// Parse the button's ID to extract the row index


String buttonId = getResources().getResourceEntryName(button.getId());
return (Integer.parseInt(buttonId.substring(6)) - 1) / 3;
}

private int getButtonCol(Button button) {

// Get the column index of the button


// Example: button1 -> col = 0
// Example: button2 -> col = 1
// Example: button4 -> col = 0
// ...

// Parse the button's ID to extract the column index


String buttonId = getResources().getResourceEntryName(button.getId());
return (Integer.parseInt(buttonId.substring(6)) - 1) % 3;

private void disableAllButtons() {


for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setEnabled(false);
}
}
}

private void enableAllButtons() {


for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setEnabled(true);
}
}
}

private void clearButtonText() {


for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setText("");
}
}
}

private void updateStatusText() {


statusTextView.setText("Player " + game.getCurrentPlayer() + "'s
turn");
}
}
class TicTacToeGame {
private char[][] board;
private char currentPlayer;

public TicTacToeGame() {
board = new char[3][3];
currentPlayer = 'X';
initializeBoard();
}

private void initializeBoard() {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}

public boolean makeMove(int row, int col) {


if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] !=
'-') {
return false;
}

board[row][col] = currentPlayer;
return true;
}

public boolean checkForWin() {


// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] != '-' && board[i][0] == board[i][1] && board[i]
[1] == board[i][2]) {
return true;
}
}

// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] != '-' && board[0][j] == board[1][j] && board[1]
[j] == board[2][j]) {
return true;
}
}

// Check diagonals
if (board[0][0] != '-' && board[0][0] == board[1][1] && board[1][1] ==
board[2][2]) {
return true;
}
if (board[0][2] != '-' && board[0][2] == board[1][1] && board[1][1] ==
board[2][0]) {
return true;
}

return false;
}

public boolean isBoardFull() {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}

public void switchPlayer() {


currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}

public char getCurrentPlayer() {


return currentPlayer;
}

public void reset() {


initializeBoard();
currentPlayer = 'X';
}
}

You might also like