How to Make a Tic-Tac-Toe Game for Android BHAILOG January 06, 2019 Android No comments Hello everyone, in this tutorial we are going to learn how to make a Tic-Tac-Toe Game for android (using Android studio). Step by step process is given in this article that will help you to make a simple Tic-Tac-Toe game in very easy way. You can also download the apk file and use it as a normal app in your android phone. Steps to Make a Tic-Tac-Toe Game App for Android 1. First of all open android studio 2. Create a new project > Start a new Android Studio Project > Now name your project “Tic-Tac-Toe”. 3. Choose a Package Name. A Package Name is a unique URL of your Application. In most cases it is the inverse of your website. For example: mohit.bhailogprogrammer.com 4. If you want you can leave it to the default package name which is “mohit.tictactoe.com”. Now hit next two times. 5. If you have made an Icon then add it otherwise go with the default one. Select Empty Activity > Now name your activity 6. After entered application name, it going to be called select the form factors your application runs on, here need to specify Minimum SDK, in our tutorial, I have declared as API15:Android 4.0.3(IceCreamSandwich) − 7. After completing this go to Package Explorer & under your Package Explorer Select src > Your package Name > activity_main.xml 8. Open the activity_main.xml file and paste the given code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_view_p1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="player 1: 0" android:textSize="30sp" android:freezesText="true"/> <TextView android:id="@+id/text_view_p2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text_view_p1" android:text="player 2: 0" android:textSize="30sp" android:freezesText="true"/> <Button android:id="@+id/button_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginTop="22dp" android:layout_marginEnd="47dp" android:layout_marginRight="47dp" android:text="reset" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:id="@+id/button_00" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_01" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_02" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:id="@+id/button_10" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_11" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_12" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:id="@+id/button_20" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_21" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> <Button android:id="@+id/button_22" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="60sp" android:freezesText="true"/> </LinearLayout> </LinearLayout> 9. After completing this go to Package Explorer & under your Package Explorer Select src > Your package Name > MainActivity.java 10. Open the MainActivity.java file and paste the given code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 package com.example.mohit.tictactoexo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.concurrent.atomic.AtomicReference; import static android.icu.lang.UCharacter.GraphemeClusterBreak.V; public class MainActivity extends AppCompatActivity implements OnClickListener { private Button[][] buttons = new Button[3][3]; private boolean player1Turn = true; private int roundCount; private int player1Points; private int Player2Points; private TextView textViewPlayer1; private TextView textViewPlayer2; private int player2Points; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textViewPlayer1 = findViewById(R.id.text_view_p1); textViewPlayer2 = findViewById(R.id.text_view_p2 ); for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ String buttonID = "button_" + i + j; AtomicReference<String> defType; int resID = getResources() .getIdentifier(buttonID, defType.get()"id", getPackageName()); buttons[i][j] = findViewById(resID); buttons[i][j].setOnClickListener(this); } } Button buttonReset = findViewById(R.id.button_reset); buttonReset.setOnClickListener(new View.OnClickListener()){ @Override public void onClick(View V){ resetGame(); } }); } @Override public void onClick(View v){ if (!((Button)v).getText().toString().equals("")){ return; } if (player1Turn){ ((Button)v).setText("X"); } else { ((Button)v).setText("O"); } roundCount++; if (checkForWin()){ if (player1Turn){ player1Wins(); } else { player2Wins(); } } else if (roundCount == 9){ draw(); } else { player1Turn = !player1Turn; } } private boolean checkForWin(){ String[][] field = new String[3][3]; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ field[i][j] = buttons[i][j].getText().toString(); } } for (int i = 0; i < 3; i++){ if (field[i][0].equals(field[i][1]) && field[i][0].equals(field[i][2]) && !field[i][0].equals("")){ return true; } } for (int i = 0; i < 3; i++){ if (field[0][i].equals(field[1][i]) && field[0][i].equals(field[2][i]) && !field[0][i].equals("")){ return true; } } if (field[0][0].equals(field[1][1]) && field[0][0].equals(field[2][2]) && !field[0][0].equals("")){ return true; } if (field[0][2].equals(field[1][1]) && field[0][2].equals(field[2][0]) && !field[0][2].equals("")){ return true; } return false; } private void player1Wins() { player1Points++; Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show(); updatePointsText(); resetBoard(); } private void player2Wins() { player2Points++; Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show(); updatePointsText(); resetBoard(); } private void draw() { Toast.makeText(this,"Draw!", Toast.LENGTH_SHORT).show(); resetBoard(); } private void updatePointsText(){ textViewPlayer1.setText("player 1: " + player1Points); textViewPlayer2.setText("player 2: " + player2Points); } private void resetBoard(){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ buttons[i][j].setText(""); } } roundCount = 0; player1Turn = true; } private void resetGame(){ player1Points = 0; player2Points = 0; updatePointsText(); resetBoard(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("roundCount", roundCount); outState.putInt("player1Points", player1Points); outState.putInt("player2Points", player2Points); outState.putBoolean("player1Turn", player1Turn); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); roundCount = savedInstanceState.getInt("roundCount"); player1Points = savedInstanceState.getInt("player1Points"); player2Points = savedInstanceState.getInt("player2Points"); player1Turn = savedInstanceState.getBoolean("player1Turn"); } } 11.Select build Android Application and build apk Show your apk If you liked the tutorial then don’t forget to comment and share! Share This: Facebook Twitter Google+ Stumble Digg Email ThisBlogThis!Share to XShare to Facebook
0 comments:
Post a Comment