How can the following algorithm be optimized?

I am currently working on a tic tac toe auto-mover game. I have come upon a decent algorithm that allows the computer to easily build towards, and claim victory spots.
The problem however is that when the opponent is dangerously close to victory, the computer continues to build towards it’s own victory instead of blocking the opponents, is there any way the following algorithm can be optimized to counter that?

The algorithm works as follows:

  1. Initialize a 3×3 integer array

  2. Loop over all possible combinations on the board (0|0, 1|1, 0|2… e.tc).

    1. If combination is a immediate victory, stop and return this position.

    2. Else initiate a loop (That loops either 1000, 10000 or 100000 times, depending on difficulty)

      1. Continually move from both sides randomly till victory is achieved. If victory is by the computer, increment the score counter.
  3. Select the board position with the highest score

This algorithm works fine and can build towards victory (example:

·|x|o    ·|x|o    ·|x|o
·|·|·    x|·|o    x|x|o
·|·|·    ·|·|·    ·|·|o

However when confronted with a board like this

·|x|o
o|x|·
·|·|·

The optimal move should be to break x’s chain, but the computer builds towards raising it’s own chain i.e moving at 1|2 (The board is 3×3, indexes start from zero. So that’d be the second row’s third coloumn).

Is there any way to optimize this algorithm such that this weakness goes away?

Sure there is a way. Your problem is that your definition of “I win” is too narrow. As you’ve pointed out, a row of three is only a win for you if your opponent doesn’t already have one – therefore your metric must track potential rows achieved by you and your opponent, and avoid states in which they get a row completed first.

1

Tic-Tac-Toe is a solved game and quite ‘easy’ to fully program or expand (its even been done in books). Pre-programming the entire game is the optimal approach.

If you were working with a larger game (such as a 20,20,5 game (these all belong to a set of games known as m,n,k games – Tic-Tac-Toe can be described as 3,3,3)) then matters of min-max trees and optimizations on them come into play. For Tic-Tac-Toe, this is overkill.

Working with just 3,3,3; if the game is represented with 0 as the empty cell, +1 as X, and -1 as O, the board has some interesting properties:

·|x|o        0 | +1 | -1
o|x|·  -->  -1 | +1 |  0
·|·|·        0 |  0 |  0

For each row, column, and diagonal, take the absolute value of the sum of the elements:

             0
            /
 0 | +1 | -1 = 0
-1 | +1 |  0 = 0
 0 |  0 |  0 = 0
 =    =    = 
 1    2    1 1

Wherever there is a 2, you want to play in that remaining spot. If there are two or more spots that can be played into, chose the one that has the sign match your own. This way, you are always guaranteed to play to win or to block if possible.

This can be used as the basis for other heuristics. Spots that have a value of 0 are rather uninteresting in that they are either blocked ( x | o | . ) or uncontested ( . | . | .). The number of contested lines that pass through a given cell then would likely make it a better play.

  1. look if you could 1-move win : play this move
  2. look if you could prevent him last-move win : play this move
  3. look if you could 2-move win : play first of these 2 moves (which one could vary according to difficulty or which one prevent the other to win)
  4. look if you could prevent him to 2-move win : play first of these 2 moves (…)
  5. play anywhere

I’m not sure what you mean by

Else initiate a loop (That loops either 1000, 10000 or 100000 times, depending on difficulty)

It seems like you’re working with a rudimentary genetic algorithm; in this case, you need a better fitness function. In particular, for each possible outcome of the game (your “all combinations”),

  • if the opponent can win in one move

should have a higher priority than

  • you can win in one move

When you say

Continually move from both sides randomly till victory is achieved. If victory is by the computer, increment the score counter.

That randomly is your problem. Try to prevent it from happening.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật