Tic Tac Toe unite is an android application game based on Artificial Intelligence .Download the game on google play store at https://play.google.com/store/apps/details?id=com.thesarthakapp.tictactoeunite
2. What is AI?
• It is an field of study which studies the goal of creating
intelligence.
• The central problems (or goals) of AI research
include reasoning, knowledge, planning, learning nat
ural language processing (communication) and the
ability to move and manipulate objects.
• There are a large number of tools used in AI, including
versions of search and mathematical
optimization, logic, method based on probability and
economics, and many others.
3. Fields in which AI is used
• Medical diagnosis
• Speech recognition
• Stock trading
• Robot control law
• Remote sensing
• Scientific discovery
• Games like tic tac toe and chess
4. Tic Tac Toe Board-(Noughts and Crosses or X’s
and O’s)
• It is two players, X and O, game who take
turns marking the spaces in a 3×3 grid. The
player who succeeds in placing three
respective marks in a horizontal, vertical, or
diagonal row wins the game.
5. About the game
• Naive counting leads to 19,683 possible board
layouts (39since each of the nine spaces can be X,
O or blank), and 362,880 (i.e. 9!) possible games
(different sequences for placing the Xs and Os on
the board).
• The game ends when three-in-a-row is obtained.
• The number of Xs is always either equal to or
exactly 1 more than the number of Os (if X
starts).
6. Strategy
• Win: If the player has two in a row, they can
place a third to get three in a row.
• Block: If the opponent has two in a row, the
player must play the third themselves to block
the opponent.
• Fork: Create an opportunity where the player
has two threats to win (two non-blocked lines
of 2)
7. Strategy : Rule Based
For Tic-tac-toe, the rules, in the order of importance, are:
• Rule 1: If I have a winning move, take it.
• Rule 2: If the opponent has a winning move, block it.
• Rule 3: If I can create a fork (two winning ways) after this move, do it.
• Rule 4: Do not let the opponent creating a fork after my move. (Opponent
may block your winning move and create a fork.)
• Rule 5: Place in the position such as I may win in the most number of
possible ways.
• Rule 1 and 2 can be programmed easily. Rule 3 is harder. Rule 4 is even
harder because you need to lookahead one opponent move, after your
move. For rule 5, you need to count the number of possible winning ways.
8. Algorithm
Data Structure
Board A nine-element vector representing the board, as shown
below. But instead of using the numbers 0, 1 or 2 in each
element, we store 2 (indicating blank), 3(indicating X) or
5(indicating O).
Turn An integer indicating which move of the game is about to be
played; 1 indicates the first move, 9 the last.
1 2 3
4 5 6
7 8 9
9. Algorithm
The main algorithm uses three sub procedures:
• Make 2: Return O if the center square of the board is blank, that is, if Board[5]=
2.otherwise, this function return blank non-corner square(2,4,6 or 8).
• Posswin(p) :Return O if player p cannot win on his next move; otherwise, it returns
the number of square that constitutes a winning move. This function will enable the
program both the win and to block the opponent’s win. Posswin operates by
checking, one at a time, each of rows, columns and diagonals. Because of the way
values a number it can test a entire row,(column or diagonal) to see if it is a possible
win by multiplying the value of its squares. If the product is 18(3x3x2), then X can
win. If the product is 50(5x5x2), then O can win. If we find a winning row we determine
which element is blank and return the number of that square.
• Go(n) : Makes a move in square n. This procedure sets board[n] to 3 if turn is odd, or
5 if turn is even. It also increments turn by 1.
10. Algorithm
• The algorithm has built in strategy for each move it may have to make. It makes the odd numbered
moves if it is playing X, the even numbered moves if it is playing O. The strategy for each turn is as
follows:
• Turn 1 Go(1)(under left corner).
• Turn 2 If Board[5] is blank, Go(5),else Go(1).
• Turn 3 If Board[5] is blank, Go(9),else Go(3).
• Turn 4 If Posswin(X) is not 0, then Go(Posswin(X))[i.e.,block opponent’s win],
else Go(Make2).
• Turn 5 If Posswin(X) is not 0, then Go(Posswin(X))[i.e.,win] else if Posswin(O)
is not 0, then Go(Posswin(O)) [i.e.,win], else if Board[7] is blank, then
Go(7), else Go(3).
• [Here the program is trying to make fork.]
• Turn 6 If Posswin(O) is not 0, then Go(Posswin(O)), else if Posswin(X) is not
0,then Go(Posswin(X)),else Go(Make2).
• Turn 7 If Posswin(X) is not 0, then Go(Posswin(X)), else if Posswin(O) is not
0,then Go(Posswin(O)),else go anywhere that is blank.
• Turn 8 If Posswin(O) is not O, then Go(Posswin(O)), else if Posswin(X) is not
0,then Go(Posswin(X)),else go anywhere that is blank.
• Turn 9 Same as Turn 7.
11. Block Diagram for Single Player game
Main Menu
One Player
Two Player
Start Game
Exit game
Exit Game
Players
Turn
Computer’s
Turn
Get player moves
Get Android moves
Check if player 1
won
Check if player 2
won
If it’s a tie
New/End
Game
Yes
Yes
No
No
Yes
12. Technologies Used
• Platform: Android OS
• IDE : Eclipse with ADT v23.2.1 (Android
Development kit)
• Server Side : Java(jdk 1.8)
• Client side :Android xml
• Project Build Target: Android 5.0.1 (API 21)
13. Android Application Development
• Android is a mobile operating system (OS) based on the Linux kernel and
currently developed by Google. With a user interface based on direct
manipulation.
• Android is made up of several necessary and dependent parts including
the following:
• A hardware reference design that describes the capabilities required of a mobile
device in order to support the software stack.
• A Linux operating system kernel that provides the low-level interface with the
hardware, memory management, and process control, all optimized for mobile
devices.
• Open source libraries for application development including SQLite, WebKit,
OpenGL, and a media manager.
• A run time used to execute and host Android applications, including the Dalvik
virtual machine and the core libraries that provide Android specific functionality.
The run time is designed to be small and efficient for use on mobile devices.
14. • Android software development kit (SDK) includes Android APIs, Development
Tools, Android Emulator, full documentation, sample code, online support.
• A user interface framework used to host and launch applications.
• Preinstalled applications shipped as part of the stack.
Android Application Development
15. What makes an Android App?
1. Activities :Your Applications presentations
layer.The UI of your application is built around
one or more extensions of the Activity class.
2. Services: “Working in the background”.Services
components run without a UI,updating your
data sources and Activities.
3. Content Poviders: manage and persisit data and
typically interact with SQL database.
4. Intents: message pass framework,work for the
joining of 2 Activities.
16. 5.Android Manifest.xml
• All the android applications will have an
AndroidManifest.xml file in the root directory.
This file will contain essential information
about the application to the Android system.
• This control file describes the nature of the
application and each of its components.
• Permissions to services are granted through
Android Manifest file.
• Ex : Services like Network Services.
18. Libraries
• appcompat v7 : used for the ActionBar APIs
in the support library.
• Google play services : Google Play services
provides you with easy access to Google
services and is tightly integrated with the
Android OS.
19. UI:setting the 3x3 board with buttons
• Table row under table layout is used to arrange
childrens (Buttons)horizontally.
• Push Button widgets are used under <tablerow >to
perform an action and are customised further.
20. UI:setting up the text view
• Text View:Displays text to the user and optionally
allows them to edit it
• 4 text views used
• Score: shows who’s turn it is ,user
or computer,and also shows the
final result.
• Human,tie and android text views
show the no. games won by
each
21. User Interface
A table row that consists
of 3x3 grid of multiple
customised Buttons
Internal Options
Menu
Text View
for Score
card
Text view
which
shows
current
status
Activity Bar
22. Setting Up a Internal Menu
•Setting up the menu resource defines an
application menu (Options Menu, Context Menu, or
submenu) that can be inflated with MenuInflater.
• Two options :
1. New Game : which starts a new game for the
user,however previous results are recorded.
2. Exit Game : Which exits the user from the game
activity to main menu activity.
• FILE LOCATION:res/menu/filename.xml
The filename will be used as the resource ID.
• RESOURCE REFERENCE:In Java: R.menu.filename
In XML: @[package:]menu.filename
23. Setting Up a Internal Menu…contd.
• Xml syntax:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/newGame"
android:showAsAction="ifRoom“/>
</menu>
24. Internal Options Menu
• To set up working of options menu Menu Inflator class is imported in the
Activity.java
• This class is used to instantiate menu XML files into Menu objects.
• When the user selects an item from the options menu (including action
items in the action bar), the system calls your
activity's onOptionsItemSelected() method.
• This method passes the MenuItem selected. You can identify the item by
calling getItemId(), which returns the unique ID for the menu item
• You can match this ID against known menu items to perform the
appropriate action. public boolean onOptionsItemSelected(MenuItem
item) {
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
25. Block Diagram for Two Player game
Main Menu
One Player
Two Player
New Game
Exit game
Exit Game
Get player 1 moves
Get player2 moves
Check if player 1
won
Check if player 2
won
If it’s a tie
New/End
Game
Player 1
Turn
Player 2
Turn Yes
Yes
No
No
Yes
26. Setting the Two player game: User
Interface
String in this text
views updates the
turn of each player
and in the end
shows the result
Score Board
3X3 board
of buttons
27. Difference from the single player game!
• In game Activity java file we change the variables
from user to Player_One and Android to
Player_two.
• Two more text views are declared in the
TictactoeActivity java file for count of the score.
• The game player_1 and player_2 within a series.
• Rest of the code is nearly the same as the single
player game had.
28. Setting Up the Main Menu
• Make a new xml file
(main_menu.xml) under
reslayout folder.
• Drag 3 buttons from
widgets section onto the
Activity screen and
arrange them vertically.
• 3 butons will be of one
player,two player and
exit game.
29. How menu buttons work?
• Create a java file (MainMenuScreen.java)for
working of buttons in main menu under
srcpackage folder.
• Set up a MainMenuScreen class which extends
Activity class.
• Activities interact with the user, so the Activity
class takes care of creating a window for you in
which you can place your UI
with setContentView(R.id.mainmenu); under
onCreate(Bundle) method( where you initialize
your activity).
30. How Menu Buttons Work?.....
((Button) findViewById(R.id.one_player)).setOnClickListener(new OnClickListener(){
public void onClick(View V) {
Log.d("DEBUG", "One Player Button Pressed!");
Intent intent = new Intent(MainMenuScreen.this, TicTacToeTutorialActivity.class);
intent.putExtra("gameType", true);
startActivityForResult(intent, 0);
}
});
1. Interface definition for a callback to be invoked when a view is clicked
2. Log class ensures that the button was clicked and Log.d sends the debug output.
3. An Intent provides a facility for performing late runtime binding between the code
in different applications. Its most significant use is in the launching of activities,
where it can be thought of as the glue between activities.
4. start another activity and receive a result back. To receive a result,
call startActivityForResult()
31. How the App is Different?
• The player(s) can compete in a series of games
and the record of games are kept under the
text views in Android Application.
• The application maintains the scoreboard
which keeps the record of winning record of
each player( human player or computer) and
also keeps the record of the number of ties in
the series of games.
32. How to set up Ads in an Application?
• Add Google Play Services Sdk in
your Project
• Create an Admob account.
• Get a publisher ID.
• Select the ad format you want in
your app
1. Horizontal Banner Ads
2. Interstitial Ads.
• The Ad unit id will be
generated.
• Add the Ad unit ID to your xml
file.
Banner Ad
34. What did we learn?
Technically
• First of all we gained additional skills in the Java
programming language that was difficult for us
before. we learned also how to use a lot of the
components in Eclipse IDE such as the debugger.
I also learned a new language that is XML, a
language I didn't know at all earlier. Finally, this
project allowed me to use my logic development
and logic programming skills acquired during my
studies.
35. Future Work
• In future in Android application development
we would like to learn the OpenGl 2D graphics
which can be implements in the application
which will improve the working and
presentation of the application.
• Also we would like to implement a 4x4 board
game.
36. Conclusion
• In the end we would like to conclude that my
aim to make this project was to research in
the field of Android Application development
and implementation of Artificial Intelligence
by developing the logic for the game. Some
scope of improvements are also there in the
project which will rectified in the future
advancements of the project. We would like to
thank all those who have helped us and
contributed in making of this project.