|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
DaSpamer Grandmaster Cheater Supreme Reputation: 52
Joined: 13 Sep 2011 Posts: 1578
|
Posted: Mon Oct 07, 2013 4:25 pm Post subject: [Lua Game] Tic Tac Toe Cheat Engine version. |
|
|
This is a script that allows you to play Tic Tac Toe via Cheat Engine.
It's simple and very easy to win.
You may improve it and post if you wish an improved version.
I've included a small details about each and each function.
Code: | ----------------------------------------
--- Tic Tac Toe Cheat Engine Edition ---
----------------------------------------
--- Written by DaSpamer ---
--- 7th October 2013 ---
--- Cheat Engine 6.3 or greater ---
----------------------------------------
TicTacToe = {
Entries = {
{ -- A used slot? (false/true);
false;
false;
false;
false;
false;
false;
false;
false;
false;
};
{ -- Player or Bot slot. (1 = player, 2 = ai);
nil;
nil;
nil;
nil;
nil;
nil;
nil;
nil;
nil;
};
};
}
function TicTacToe:Start()
-- Defining the vars, creating the form,the image,canvas,brush,pen and ai timer
-- Then calling the reset function to draw and prepare the game and displaying the form.
self.AIScore = 0;
self.PlayerScore = 0;
self.gameover = false;
self.canclick = true;
--
self.form = createForm(false);
self.form.width = 600;
self.form.height = 600;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
setProperty(self.form, "BiDiMode", "bdLeftToRight");
setProperty(self.form , "Position", "poScreenCenter")
self.image = createImage(self.form);
self.image.width = 600;
self.image.height = 600;
image_stretch(self.image, true);
setProperty(self.image, 'Anchors','akTop,akLeft,akRight,akBottom');
self.image.onClick = function(sender) self:Click(sender); end;
self.canvas = self.image.getCanvas();
self.brush = self.canvas.getBrush();
-- Form color.
self.brush.Color = 0xF0F0F0;
self.canvas.fillRect(self.canvas, 0, 0, self.form.width, self.form.width);
self.pen = self.canvas.getPen();
self.ai = createTimer(nil,false);
-- Change inteval for faster/slower ai turns.
self.ai.Interval = 500;
self.ai.onTimer = function (sender) self:Click(sender); end;
self.ai.Enabled = false;
self:Reset();
self.form.show();
end
function TicTacToe:CheckWin()
-- Checking if the var self.gameover is defined as true already and if yes, showing message that game ended and asking if the player wants to play again
if (self.gameover) then
local over = messageDialog("Game ended!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
if (over == mrYes) then
self:Reset();
return true
end
return true;
end
-- Checking if combo of 3 entries (1,2,3 or 4,5,6 or 7,8,9 or 1,4,7 or 2,5,8 or 3,6,9 or 1,5,9 or 3,5,7) are used
-- if yes, checking the id of the slot is user ID or ai ID
-- 1 = user ID
-- 2 = ai ID
-- Increasing user/ai score if user/ai won and displaying a message and option to reset.
-- If all 9 slots does not have any winning combo, then displaying message that nobody won and allowing user to restart.
if (self.Entries[1][1] and self.Entries[1][2] and self.Entries[1][3]) then
if (self.Entries[2][1] == 2 and self.Entries[2][2] == 2 and self.Entries[2][3] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][1] == 1 and self.Entries[2][2] == 1 and self.Entries[2][3] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][4] and self.Entries[1][5] and self.Entries[1][6]) then
if (self.Entries[2][4] == 2 and self.Entries[2][5] == 2 and self.Entries[2][6] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][4] == 1 and self.Entries[2][5] == 1 and self.Entries[2][6] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][7] and self.Entries[1][8] and self.Entries[1][9]) then
if (self.Entries[2][7] == 2 and self.Entries[2][8] == 2 and self.Entries[2][9] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][7] == 1 and self.Entries[2][8] == 1 and self.Entries[2][9] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][1] and self.Entries[1][4] and self.Entries[1][7]) then
if (self.Entries[2][1] == 2 and self.Entries[2][4] == 2 and self.Entries[2][7] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][1] == 1 and self.Entries[2][4] == 1 and self.Entries[2][7] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][2] and self.Entries[1][5] and self.Entries[1][8]) then
if (self.Entries[2][2] == 2 and self.Entries[2][5] == 2 and self.Entries[2][8] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][2] == 1 and self.Entries[2][5] == 1 and self.Entries[2][8] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][3] and self.Entries[1][6] and self.Entries[1][9]) then
if (self.Entries[2][3] == 2 and self.Entries[2][6] == 2 and self.Entries[2][9] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][3] == 1 and self.Entries[2][6] == 1 and self.Entries[2][9] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][1] and self.Entries[1][5] and self.Entries[1][9]) then
if (self.Entries[2][1] == 2 and self.Entries[2][5] == 2 and self.Entries[2][9] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][1] == 1 and self.Entries[2][5] == 1 and self.Entries[2][9] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][3] and self.Entries[1][5] and self.Entries[1][7]) then
if (self.Entries[2][3] == 2 and self.Entries[2][5] == 2 and self.Entries[2][7] == 2) then
local Win = messageDialog("You lost the game!\nWould you like to retry?",mtInformation, mbYes,mbNo);
self.AIScore = self.AIScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
elseif (self.Entries[2][3] == 1 and self.Entries[2][5] == 1 and self.Entries[2][7] == 1) then
local Win = messageDialog("You WON the game!\nWould you like to play again?",mtInformation, mbYes,mbNo);
self.PlayerScore = self.PlayerScore + 1;
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
self.gameover = true;
if (Win == mrYes) then
self:Reset();
end
return true;
end
end
if (self.Entries[1][1] and self.Entries[1][2] and self.Entries[1][3] and self.Entries[1][4] and self.Entries[1][5] and self.Entries[1][6] and self.Entries[2][7] and self.Entries[2][8] and self.Entries[2][9]) then
self.form.Caption = 'Tic Tac Toe | Player - ' .. self.PlayerScore .. ' Computer - ' .. self.AIScore .. ' |';
local tie = messageDialog("Nobody won, would you like to to play again?",mtInformation, mbYes,mbNo);
if tie == mrYes then
self:Reset();
end
return true
end
end
function TicTacToe:Reset()
-- Clearing the canvas, resetting game vars and re-creating the slots.
self.canvas.clear();
self.gameover = false;
setProperty(self.image , "Cursor", "-21")
self.canclick = true;
self.ai.Enabled = false;
self.brush.Color = 0xF0F0F0;
for i=1,9 do
self.Entries[1][i] = false;
self.Entries[2][i] = nil;
end
self.pen.width = 2;
self.pen.Color = 0x000000;
self.canvas.setPenPosition(25,25);
self.canvas.lineTo(575,25);
self.canvas.lineTo(575,575);
self.canvas.lineTo(25,575);
self.canvas.lineTo(25,25);
self.canvas.setPenPosition(25,(self.image.width /3) * 1);
self.canvas.lineTo((self.image.width /3) * 1,575);
self.canvas.setPenPosition(25,(self.image.width /3) * 2);
self.canvas.lineTo((self.image.width /3) * 2,575);
self.canvas.setPenPosition((self.image.height /3) * 1, 25);
self.canvas.lineTo(575,(self.image.height /3) * 1);
self.canvas.setPenPosition((self.image.height /3) * 2, 25);
self.canvas.lineTo(575,(self.image.height /3) * 2);
end
function TicTacToe:Click(sender)
-- Image click, aka the canvas click
-- Checking if game ended.
local isended = self:CheckWin()
if (isended) then
return
end
-- Checking if user can click and the C.E Image was the sender (Which is the result of clicking on it);
if (sender.getClassName() == 'TCEImage' and self.canclick) then
local status,slot = self:CheckSlot(getMousePos());
-- Checking if user clicked on an actual slot and sending the the function the user position according the screen.
if (status) then
-- Checking if the slot is not used
if not(self.Entries[1][slot]) then
-- Displaying slot
self:Draw(slot, sender.getClassName());
if (self:CheckWin()~=true) then
-- Checking if player has won the game, if not let's the AI to take a turn,prevents from player from clicking again
-- And changing cursor
self.ai.Enabled = true;
setProperty(self.image , "Cursor", "0")
self.canclick = false
end
end
end
-- if sender isn't TCEImage, that means the player didn't click on the image.
elseif (sender.getClassName() ~= 'TCEImage') then
-- Disabling AI Timer.
self.ai.Enabled = false;
-- Creating temporary table to store all valid slots.
local temp = {}
-- Checking all the 9 slots that their values are false and nil (false = used, nil = owner);
for i = 1,9 do
if (self.Entries[1][i] == false and self.Entries[2][i] == nil) then
-- Storing the the index of the slot in the temp table
temp[#temp+1] = i
end
end
-- Picking random number
local result = math.random(#temp);
-- Taking the used&owner id (number 1 to 9);
self:Draw(temp[result]);
-- Checking if AI won, allowing user to click again and changing cursor when hovering the image
self:CheckWin()
self.canclick = true;
setProperty(self.image , "Cursor", "-21")
end
end
function TicTacToe:Draw(Selection , Sender)
if (Sender == 'TCEImage') then
-- Drawning X (Player Turn);
self.pen.width = 5;
self.pen.Color = 0x000000FF;
if (Selection == 1) then
self.canvas.setPenPosition(45,45);
self.canvas.lineTo(180,180);
self.canvas.setPenPosition(45,180);
self.canvas.lineTo(45,180);
self.Entries[1][1] = true;
self.Entries[2][1] = 1;
elseif (Selection == 2) then
self.canvas.setPenPosition(45,235);
self.canvas.lineTo(370,180);
self.canvas.setPenPosition(45,370);
self.canvas.lineTo(235,180);
self.Entries[1][2] = true;
self.Entries[2][2] = 1;
elseif (Selection == 3) then
self.canvas.setPenPosition(45,420);
self.canvas.lineTo(555,180);
self.canvas.setPenPosition(45,555);
self.canvas.lineTo(420,180);
self.Entries[1][3] = true;
self.Entries[2][3] = 1;
elseif (Selection == 4) then
self.canvas.setPenPosition(235,45);
self.canvas.lineTo(180,370);
self.canvas.setPenPosition(370,45);
self.canvas.lineTo(180,235);
self.Entries[1][4] = true;
self.Entries[2][4] = 1;
elseif (Selection == 5) then
self.canvas.setPenPosition(235,235);
self.canvas.lineTo(370,370);
self.canvas.setPenPosition(370,235);
self.canvas.lineTo(370,235);
self.Entries[1][5] = true;
self.Entries[2][5] = 1;
elseif (Selection == 6) then
self.canvas.setPenPosition(235,420);
self.canvas.lineTo(555,370);
self.canvas.setPenPosition(370,420);
self.canvas.lineTo(555,235);
self.Entries[1][6] = true;
self.Entries[2][6] = 1;
elseif (Selection == 7) then
self.canvas.setPenPosition(420,45);
self.canvas.lineTo(180,555);
self.canvas.setPenPosition(555,45);
self.canvas.lineTo(180,420);
self.Entries[1][7] = true;
self.Entries[2][7] = 1;
elseif (Selection == 8) then
self.canvas.setPenPosition(420,235);
self.canvas.lineTo(370,555);
self.canvas.setPenPosition(555,235);
self.canvas.lineTo(370,420);
self.Entries[1][8] = true;
self.Entries[2][8] = 1;
elseif (Selection == 9) then
self.canvas.setPenPosition(420,420);
self.canvas.lineTo(555,555);
self.canvas.setPenPosition(555,420);
self.canvas.lineTo(555,420);
self.Entries[1][9] = true;
self.Entries[2][9] = 1;
end
else
-- Drawining circle (CPU turn);
self.pen.width = 5;
self.pen.Color = 0x00FF0000;
if (Selection == 1) then
self.canvas.ellipse(45, 45,180, 180);
self.Entries[1][1] = true;
self.Entries[2][1] = 2;
elseif (Selection == 2) then
self.canvas.ellipse(235, 45, 370, 180);
self.Entries[1][2] = true;
self.Entries[2][2] = 2;
elseif (Selection == 3) then
self.canvas.ellipse(420, 45, 555, 180);
self.Entries[1][3] = true;
self.Entries[2][3] = 2;
elseif (Selection == 4) then
self.canvas.ellipse(45, 235,180, 370);
self.Entries[1][4] = true;
self.Entries[2][4] = 2;
elseif (Selection == 5) then
self.canvas.ellipse(235, 235, 370, 370);
self.Entries[1][5] = true;
self.Entries[2][5] = 2;
elseif (Selection == 6) then
self.canvas.ellipse(420, 235, 555, 370);
self.Entries[1][6] = true;
self.Entries[2][6] = 2;
elseif (Selection == 7) then
self.canvas.ellipse(45, 425,180, 555);
self.Entries[1][7] = true;
self.Entries[2][7] = 2;
elseif (Selection == 8) then
self.canvas.ellipse(235, 425, 370, 555);
self.Entries[1][8] = true;
self.Entries[2][8] = 2;
elseif (Selection == 9) then
self.canvas.ellipse(420, 425, 555, 555);
self.Entries[1][9] = true;
self.Entries[2][9] = 2;
end
end
end
function TicTacToe:CheckSlot(Y,X)
-- Checking the slots location and check the mouse position.
-- There's some bug i guess with the self.form.left and self.form.top.
-- It does not return the current form position,so I gotta use contol_getPosition.
local fy, fx = control_getPosition(self.form)
local Slot_X1_min = fx + 50;
local Slot_X1_max = fx + 25 + (self.image.width /3) * 1;
local Slot_X2_min = fx + 25 + (self.image.width /3) * 1;
local Slot_X2_max = fx + 25 + (self.image.width /3) * 2;
local Slot_X3_min = fx + 25 + (self.image.width /3) * 2;
local Slot_X3_max = fx + 25 + self.image.width - 25;
local Slot_Y1_min = fy + 30;
local Slot_Y1_max = fy + (self.image.height / 3) * 1;
local Slot_Y2_min = fy + (self.image.height / 3) * 1;
local Slot_Y2_max = fy + (self.image.height / 3) * 2;
local Slot_Y3_min = fy + (self.image.height / 3) * 1;
local Slot_Y3_max = fy + 30 + self.image.height - 50;
-- Alternatively I could do this:
--[[
local fy, fx = control_getPosition(self.form)
local Slot_X1_min = fx + 25 + 25;
local Slot_X1_max = fx + 25 + 25 + 199;
local Slot_X2_min = fx + 25 + 25 + 200;
local Slot_X2_max = fx + 25 + 25 + 399;
local Slot_X3_min = fx + 25 + 25 + 400;
local Slot_X3_max = fx + 25 + 25 + 550;
local Slot_Y1_min = fy + 3 + 25;
local Slot_Y1_max = fy + 3 + 25 + 199;
local Slot_Y2_min = fy + 3 + 25 + 200;
local Slot_Y2_max = fy + 3 + 25 + 399;
local Slot_Y3_min = fy + 3 + 25 + 400;
local Slot_Y3_max = fy + 3 + 25 + 550;
--]]
if (X > Slot_X1_min and X < Slot_X1_max and Y > Slot_Y1_min and Y < Slot_Y1_max) then
return true,1;
elseif (X > Slot_X1_min and X < Slot_X1_max and Y > Slot_Y2_min and Y < Slot_Y2_max) then
return true,2;
elseif (X > Slot_X1_min and X < Slot_X1_max and Y > Slot_Y3_min and Y < Slot_Y3_max) then
return true,3;
elseif (X > Slot_X2_min and X < Slot_X2_max and Y > Slot_Y1_min and Y < Slot_Y1_max) then
return true,4;
elseif (X > Slot_X2_min and X < Slot_X2_max and Y > Slot_Y2_min and Y < Slot_Y2_max) then
return true,5;
elseif (X > Slot_X2_min and X < Slot_X2_max and Y > Slot_Y3_min and Y < Slot_Y3_max) then
return true,6;
elseif (X > Slot_X3_min and X < Slot_X3_max and Y > Slot_Y1_min and Y < Slot_Y1_max) then
return true,7;
elseif (X > Slot_X3_min and X < Slot_X3_max and Y > Slot_Y2_min and Y < Slot_Y2_max) then
return true,8;
elseif (X > Slot_X3_min and X < Slot_X3_max and Y > Slot_Y3_min and Y < Slot_Y3_max) then
return true,9;
end
return false;
end
TicTacToe:Start() |
_________________
Last edited by DaSpamer on Sat Oct 19, 2013 1:31 pm; edited 1 time in total |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Mon Oct 07, 2013 6:21 pm Post subject: |
|
|
You didn't implement minimax algorithm.
Winning is easy as piece of cake.
_________________
|
|
Back to top |
|
|
RedGuardian How do I cheat? Reputation: 0
Joined: 06 Oct 2013 Posts: 3
|
Posted: Mon Oct 07, 2013 8:45 pm Post subject: |
|
|
can u show me how to make form with AlwaysOnTop attribute?
i've tried setProperty(Form1, "FormStyle", "fsSystemStayOnTop") only work in form editor but when i compiled it wont stay on top anymore.
|
|
Back to top |
|
|
DaSpamer Grandmaster Cheater Supreme Reputation: 52
Joined: 13 Sep 2011 Posts: 1578
|
Posted: Thu Oct 10, 2013 1:47 pm Post subject: |
|
|
RedGuardian, make sure that nothing else 'sets' it's style.
@mgr.inz.Player, I'll work on it bit more :p , I was mainly trying the brush,pen and canvas features.
_________________
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|