Zuma Game Algorithm

Zuma Game Algorithm

Jump To:

Objective

Main Objective of this blog post is to give you a basic idea about how to work with Bezier Curve In Games.

Step 1 Introduction

Bezier curves are the most fundamental curves, used generally in computer graphics and image processing. Bezier curves can be used for creating smooth curved roads, curved paths just like zuma game, curved shaped rivers, etc. in your game.

A Bezier curve is defined by a set of control points P0 through Pn, where n is called its order (n = 1 for linear, 2 for quadratic, etc.). The first and last control points are always the end points of the curve; however, the intermediate control points (if any) generally do not lie on the curve.

  • Bezier curve containing two control points i.e. n = 2 is called a Linear Bezier Curve
  • Bezier curve containing three control points i.e. n = 3 is called a Quadratic Bezier Curve
  • Bezier curve containing four control points i.e. n = 4 is called a Cubic Bezier Curve and so on.

Sep 15, 2018  Our best puzzle game! Zuma Deluxe - Stage 1 - 13: All Game - Adventure Mode Zuma Deluxe - Stage 1: Temple of Zukulkan - Adventure Mode Level 1 - 1 (SPIRAL OF DOOM) Level 1. Problem statement: Consider a row of n coins of values v1. Vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin.

Bezier function, that returns points on bezier curve uses concept of linear interpolation as base. So, Let’s understand what is Linear Interpolation first.

Step 2 Linear Intrepolation

Linear interpolation between two points means getting interpolated point for different values of t between those two points, where 0 < t < 1, just like Mathf.Lerp.

Formula for interpolated point, P between P0 and P1 can be written as,

  • P = P0 + t(P1 – P0) , 0 < t < 1

Here, for getting interpolated point we are adding tth fraction of distance between those two points to P0. So,

  • For t=0,P = P0.
  • For t=1, P = P1.
  • For t=0.5, P = Intermediate point between P0 and P1.

Step 3 Linear Bezier Curves

Linear Bezier curve has two control points. For given two points P0 and P1, a Linear Bezier curve is simply a straight line between those two points. The curve is equivalent to linear interpolation and is given by,

  • B(t) = P0 + t(P1 – P0) = (1-t) P0 + tP1 , 0 < t < 1

Animation of how a linear bezier curve is calculated is shown below:

Step 4 Quadratic Bezier Curves

Quadratic bezier curve has three control points. Quadratic Bezier curve is a point-to-point linear interpolation of two Linear Bezier Curves. For given three points P0, P1 and P2, a quadratic bezier curve is a linear interpolation of two points, got from Linear Bezier curve of P0 and P1 and Linear Bezier Curve of P1 and P2. So, Quadratic bezier curve is given by,

  • B(t) = (1-t) BP0,P1(t) + t BP1,P2(t), 0 < t < 1
  • B(t) = (1-t) [(1-t) P0 + tP1] + t [(1-t) P1 + tP2] , 0 < t < 1

By rearranging the above equation,

  • B(t) = (1-t)2P0 + 2(1-t)tP1 + t2P2 , 0 < t < 1

Animation of how quadratic bezier curve is calculated is shown below:

Step 5 Cubic Bezier Curves

Cubic Bezier curve has four control points. Quadratic bezier curve is a point-to-point linear interpolation of two Quadratic Bezier curves. For given four points P0, P1, P2 and P3, a cubic bezier curve is a linear interpolation of two points, got from Quadratic Bezier curve of P0, P1and P2 and Quadratic Bezier Curve of P1, P2 and P3. So, Cubic bezier curve is given by,

  • B(t) = (1-t) BP0,P1,P2(t) + t BP1,P2,P3(t), 0 < t < 1
  • B(t) = (1-t) [(1-t)2P0 + 2(1-t)tP1 + t2P2] + t [(1-t)2P1 + 2(1-t)tP2 + t2P3] , 0 < t < 1

By rearranging the above equation,

  • B(t) = (1-t)3P0 + 3(1-t)2tP1 + 3(1-t)t2P2 + t3P3 , 0 < t < 1

Animation of how cubic bezier curve is calculated is shown below:

So, In general the bezier curve of degree n can be defined as a point-to-point linear interpolation of two points obtained from two corresponding bezier curves of degree n-1.

Step 6 Demo

In most of applications either quadratic or cubic bezier function is used. However, you can always make use of higher degree bezier function to draw more complicated curves but calculation of higher degree bezier function is more complex and increases processing overhead. So, instead of using higher degree bezier function for drawing more complicated curves, you can use either quadratic or cubic bezier function multiple times. Here, I have created one demo and drawn shape curve, using cubic bezier function two times in a loop as shown below.

Game

To create a curve as shown above, create a scene as shown below:

Now, attach Bezier.cs script to Bezier Manager.

Bezier.cs:

Here, CalculateCubicBezierPoint function is an implementation of Cubiz Bezier function which I had explained above. DrawCurve function draws two cubic bezier curves.

  1. Between P0, P0- control Point1, P1- control Point1 and P1.
  2. Between P1, P1- control Point1, P0- control Point2 and P0.

Any control point can handle the curvature of its corresponding curve. You can change the curve at any time by dragging any control point as shown below:

I hope you find this blog post is very helpful while working with Bezier Curve in Unity. Let me know in comments if you have any question regarding Unity.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

Created on : 30 July 2015

Zuma Game Algorithm Game

Amit is proficient with C#, Unity. He has experience with different programming languages and technologies. He is very passionate about game development and the gaming industry, and his objective is to help build profitable, interactive entertainment.

Zuma Game Algorithm Free

PREVIOUS POST
Resolution Independent Vertical Horizontal Layout in Unity UI
NEXT POST
How to ace the FINITE State Machine Model in Unity AI Implementation

Problem:

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.

Zuma Game Algorithm Download

Examples:
Input: “WRRBBW”, “RB”
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW

Input: “WWRRBBWW”, “WRBRW”
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

Input:“G”, “GGGGG”
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty

Input: “RBYYBBRRB”, “YRBGB”
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

Zuma Game Algorithm Game

Note:

Zuma game algorithm download
  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. The number of balls on the table won’t exceed 20, and the string represents these balls is called “board” in the input.
  3. The number of balls in your hand won’t exceed 5, and the string represents these balls is called “hand” in the input.
  4. Both input strings will be non-empty and only contain characters ‘R’,’Y’,’B’,’G’,’W’.

Idea: Search

Solution1: C++ / Search

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
// Runtime: 3 ms
public:
vector<int>h(128,0);
returndfs(board,h);
private:
// Return the min # of balls needed in hand to clear the board.
intdfs(conststring&board,vector<int>&hand){
inti=0;
while(i<board.size()){
// board[i] ~ board[j - 1] have the same color
// Number of balls needed to clear board[i] ~ board[j - 1]
// Have sufficient balls in hand
// Remove board[i] ~ board[j - 1] and update the board
stringnb=update(board.substr(0,i)+board.substr(j));
// Find the solution on new board with updated hand
intr=dfs(nb,hand);
// Recover the balls in hand
}
}
}
// Update the board by removing all consecutive 3+ balls.
stringupdate(stringboard){
while(i<board.size()){
while(j<board.size()&&board[i]board[j])++j;
board=board.substr(0,i)+board.substr(j);
}else{
}
returnboard;
};

Zuma Game Free Download

请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.

Buy anything from Amazon to support our website
您可以通过在亚马逊上购物(任意商品)来支持我们

Zuma Game Algorithm Download

Venmo
huahualeetcode