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

Lecture 8

Uploaded by

kkyu01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 8

Uploaded by

kkyu01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ENE 2017

Engineering Programming
Prof. Keonwook Kim
Division of Electronics and Electrical Engineering
Dongguk University, Seoul, Korea

Ch.11 TicTacToe Magic


Three simple games are related in a surprising way. And, the
programming of the game play is instructive.
Experiments with MATALB – Cleve Moler

TicTacToe magic
• Rule
• The game involves two players.
• You start by listing the single digit numbers from 1 to 9.
• You then take turns selecting numbers from the list, attempting to
acquire three numbers that add up to 15.
• Each number can be chosen only once.
• You may eventually acquire more than three numbers, but you must
use exactly three of them to total 15.
• If neither player can achieve the desired total, the game is a draw.

Experiments with MATALB – Cleve Moler

TicTacToe magic
Experiments with MATALB – Cleve Moler

TicTacToe magic
• MATLAB
• tictactoe program

• Three-step strategy
• If possible, make a winning move.
• If necessary, block a possible winning move by the opponent.
• Otherwise, pick a random empty square.

Experiments with MATALB – Cleve Moler

TicTacToe magic
• TicTacToe played on a magic square
• The rows, columns and main diagonals of the magic square provide
all possible ways of having three distinct numbers that sum to 15.
• Winning moves correspond to winning moves in TicTacToe.
• Actually the same game with different displays.
Experiments with MATALB – Cleve Moler

Game play
• You are the green player and the computer is the blue player.
• The state of the game is carried in a 3-by-3 matrix X whose
entries are +1 for cells occupied by green, -1 for cells
occupied by blue, and 0 for as yet unoccupied cells.

Experiments with MATALB – Cleve Moler

Game play
• looks for any column, row, or diagonal whose elements sum
to 3*p where p = 1 for green and p = -1 for blue.
• The only way a sum can be 3*p is if all three elements are equal to
p.
Experiments with MATALB – Cleve Moler

Game play

Experiments with MATALB – Cleve Moler

Game play
Experiments with MATALB – Cleve Moler

Game play

You might also like