Optimization of Java code using the example of Tic Tac Toe, an absolutely powerful game. Part 1

Optimization of Java code using the example of Tic Tac Toe, an absolutely powerful game. Part 1

  1. Part 1. This. Optimization of main.xml. Interface.

  2. Part 2. Optimizing mainactivity.java. Program logic.

p align=”justify”> Program code optimization is one of the most important aspects of programming.

Why is code optimization necessary?

  1. The program starts working faster.

  2. Code readability increases.

  3. Individual functions are separated into meaningful blocks, which allows you to use them in other programs.

I will consider the optimization of the program code using the example of the game 🎮👾Ticks and tic-tac-toe🎮👾.

The game is a member of the Puzzle and Board Game class, just like Checkers, Chess, and Go. The development of the theory in Khrestiki-Nolyka has already reached the level, unlike in chess, which has led the game to the state nobody’s deaththat is, anyone who owns the game, regardless of the skill of the opponent, will be able to draw the game in the worst case scenario.

I will create a program that, regardless of the turn of the move, can win or get a draw.

The game is written in AIDEin Java.

Tic Tac Toe game plan

  1. The program makes the first move in odd lots and the second in even lots.

  2. With the correct play of the human opponent, the game ALWAYS will be tied.

  3. The program will not be able to lose for any human strategies.

I will create a program with the so-called pseudo intelligenceThat is, the program will act intelligently and walk randomly.

Although it is possible to create a game with pure AI, but this is what I will do now.

Optimization of main.xml. Interface

Most often, programmers do not optimize the Interface. And why, as everything is so obvious?

But still, it is worth optimizing the Interface. This will lead to a reduction in code, which means that the readability of the program will increase. And there will be more information on the screen, which is very convenient!

I’ll give you the code first, followed by the optimization.

Creating a game cap

Title and Help Button

<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:gravity="center">
			<TextView android:text="@string/name_game"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textColor="#0000FF"
				android:background="#FFFF00"
				android:textSize="36sp"/>
			<ImageView android:id="@+id/btnImgHelp"
				android:layout_width="50dp"
				android:layout_height="50dp"
				android:src="https://habr.com/ru/articles/788854/@drawable/btn_help"
				android:onClick="onClickImgBtn"/>
		</LinearLayout>

A picture of a player, a smartphone and a place for a score

<LinearLayout android:id="@+id/LLStr_Game_Data"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="horizontal">
			<TableLayout android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:gravity="center"
				android:stretchColumns="*">
				<TableRow android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:gravity="center">
					<ImageView android:layout_width="50dp"
						android:layout_height="50dp"
						android:src="https://habr.com/ru/articles/788854/@drawable/player"/>
					<ImageView android:layout_width="50dp"
						android:layout_height="50dp"
						android:src="https://habr.com/ru/articles/788854/@drawable/dragon"/>
				</TableRow>
				
				<TableRow android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:gravity="center">
					<TextView android:id="@+id/TVGamer"
						android:layout_width="wrap_content"
						android:layout_height="wrap_content"
						android:textColor="#0000FF"
						android:background="#00FF00"
						android:textSize="12sp"
						android:drawableLeft="@drawable/money"
						android:gravity="center_vertical"/>
					<TextView android:text="0"
						android:layout_width="wrap_content"
						android:layout_height="wrap_content"
						android:textColor="#00FF00"
						android:background="#0000FF"
						android:textSize="12sp"
						android:drawableLeft="@drawable/money"
						android:gravity="center_vertical"/>
				</TableRow>
			</TableLayout>
		</LinearLayout>

As you can see the lines of code: 15 + 41 = 56!

Agree that although there is code reading, but the space is taken up by something too much! If it is possible and not essential in the code editor, then when you write the code on the site, it will still be better if there is less of this same code!

So will take care of Optimization!

About analyzing the code and we will see that:

<TableRow android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:gravity="center">

<ImageView android:layout_width="50dp"
						android:layout_height="50dp"

These two pieces of code are repeated. There are other repetitions, but let’s take our time and do everything in order so as not to get confused.

In Java, we can export everything that is repeated to a separate file.

  1. This is more convenient: readability increases.

  2. Use in other programs – just by copying one file!

What will we do!

Let’s create a file: myres_styles.xml – My Style Resources.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyStyle">
        <item name="android:layout_width">wrap_content</item> 
        <item name="android:layout_height">wrap_content</item>
	</style>
	
	<style name="MyStyleF"> 
        <item name="android:layout_width">fill_parent</item> 
        <item name="android:layout_height">wrap_content</item>
	</style>

<style name="MyTableRow">
<item name="android:layout_width">wrap_content</item>
					<item name="android:layout_height">wrap_content</item>
					<item name="android:gravity">center</item>
</style>

</resources>

And now let’s apply the Style: MyTableRow to element:

<TableRow style="@style/MyTableRow">

Note that! There were 3 + 3 = 6 rows, but there were only 2!

Let’s create another Style for Image:

<style name="MyImgV">
    <item name="android:layout_width">50dp</item>
	<item name="android:layout_height">50dp</item>
</style>

I will show the rest of the styles, and then I will show the optimization itself.

<style name="MyTxtV">
    <item name="android:layout_width">wrap_content</item>
	<item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">12sp</item>
	<item name="android:gravity">center_vertical</item>
</style>

<style name="MyStyleF.LL_TL"> 
     <item name="android:gravity">center</item>
</style>

Cap. There were 15 lines of code. It became 11.

<LinearLayout style="@style/MyStyleF.LL_TL">
			<TextView android:text="@string/name_game"
				style="@style/MyStyle"
                android:textColor="#0000FF"
				android:background="#FFFF00"
				android:textSize="36sp"/>
			<ImageView android:id="@+id/btnImgHelp"
				style="@style/MyImgV"
				android:src="https://habr.com/ru/articles/788854/@drawable/btn_help"
				android:onClick="onClickImgBtn"/>
		</LinearLayout>

Image of players. There were 41 lines. It became 26!

<LinearLayout android:id="@+id/LLStr_Game_Data"
    style="@style/MyStyle"
	android:orientation="horizontal">
	<TableLayout style="@style/MyStyleF.LL_TL"
	     android:stretchColumns="*">
         <TableRow style="@style/MyTableRow">			
              <ImageView style="@style/MyImgV"
                  android:src="https://habr.com/ru/articles/788854/@drawable/player"/>
	          <ImageView style="@style/MyImgV"
		          android:src="https://habr.com/ru/articles/788854/@drawable/dragon"/>
         </TableRow>
				
         <TableRow style="@style/MyTableRow">			
					<TextView android:id="@+id/TVGamer"
						style="@style/MyTxtV"
						android:textColor="#0000FF"
						android:background="#00FF00"
						android:drawableLeft="@drawable/left"/>
					<TextView style="@style/MyTxtV"
                        android:text="0"
						android:textColor="#00FF00"
						android:background="#0000FF"
						android:drawableLeft="@drawable/flag"/>
				</TableRow>
			</TableLayout>
		</LinearLayout>

It was: 15 + 41 = 56.

It became: 11 + 26 = 37

Savings: 56 – 37 = 19.

As you can see, even in this example:

  1. Optimizing your application code is very beneficial.

  2. This reduces the size of the program.

  3. It becomes more readable and without redundant code.

This was one part: Optimizing the Interface.

In the 2nd part there will be:

  • The code itself

  • Code optimization

  • Solution of the following task:

  • Reduce the game to a win or a draw and never lose!

Continuation in 2 parts.

Related posts