Tin học Game Caro viết bằng C++

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,621
744
21
Phú Yên
Trường THPT Lương Văn Chánh
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Share vài trăm dòng Code game cho những bạn muốn lập trình game bằng C++. Ai có hứng tìm hiểu thì trao đổi với mình.
Demo game :D
upload_2019-10-11_15-52-23.png

Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
        int bkc = 0; //background color = black
        int btnc = 6; // button color = orange
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
        
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y-i].player == pl) count++;
        else if (ListSq[x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y+i].player == pl) count++;
        else if (ListSq[x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y+i].player == pl) count++;
        else if (ListSq[x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y-i].player == pl) count++;
        else if (ListSq[x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
    
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
    
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListSq[x-1][y-1].player = null;
            ListSq[x-1][y-1].value = null;
        }
    }
    curSq.player = P1;
    curSq.value = VX;
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
void DrawWin(int x, int y)
{
    int pl = ListSq[x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        {   
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y-i].player == pl) Drawchar(x-i,y-i,ListSq[x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y+i].player == pl) Drawchar(x+i,y+i,ListSq[x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y+i].player == pl) Drawchar(x-i,y+i,ListSq[x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y-i].player == pl) Drawchar(x+i,y-i,ListSq[x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListSq[x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListSq[x/w][y/h].value = curSq.value;
            ListSq[x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
    
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    {   
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    {   
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListSq[x][y].value = null;
    ListSq[x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
    }
    
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}

Lưu ý là phải cài thư viện Winbgim.h vào mới xài được nha....không thì lại bảo code mình lỗi :D
 

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,621
744
21
Phú Yên
Trường THPT Lương Văn Chánh
Đây là code game có dùng trí tuệ nhân tạo. Mình thêm thuật toán AI và thay đổi code ở trên mọt chút là tạo được game này. Tuy chơi nó hơi ngu một tí nhưng mà vẫn thắng được một số người chơi ở mức cơ bản.
Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
const int bkc = 0; //background color = black
const int btnc = 6; // button color = orange
const int level = 2; //SET LEVEL HERE
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
        
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare ListBoard[level+1][col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
int AtkArray[9] = {0,1,2,7,100,200,210,220,230};
int DefArray[9] = {0,1,2,7,30,40,50,60,70};
/*----------------------------------------------------------------*/

/*--------------------------AI------------------------------------*/
void BoardToBoard(int b1, int b2)
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            ListBoard[b2][x][y].value = ListBoard[b1][x][y].value;
            ListBoard[b2][x][y].player = ListBoard[b1][x][y].player;
        }
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
int AIHori(int x, int y,int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIVerti(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIPCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y-i].player == player) count++;
        else if (ListBoard[cboard][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y+i].player == player) count++;
        else if (ListBoard[cboard][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y+i].player == player) count++;
        else if (ListBoard[cboard][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y-i].player == player) count++;
        else if (ListBoard[cboard][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRepeat(int x,int y,int pl,int b)
{
    int max = -9999;
    BoardToBoard(b-1,b);
    ListBoard[b][x][y].player = (P1 + P2) - pl;
    int scoreDef = DefArray[AIHori(x,y,b)] + DefArray[AIVerti(x,y,b)] + DefArray[AIPCross(x,y,b)] + DefArray[AIRCross(x,y,b)];
    ListBoard[b][x][y].player = pl;
    int scoreAtk = AtkArray[AIHori(x,y,b)] + AtkArray[AIVerti(x,y,b)] + AtkArray[AIPCross(x,y,b)] + AtkArray[AIRCross(x,y,b)];
    int score = scoreDef + scoreAtk;
    if (b < level)
    {
        int sc;
        FOR(x1,0,col-1)
        {
            FOR(y1,0,row-1)
            if (ListBoard[b][x1][y1].player == null)
            {
                sc = AIRepeat(x1,y1,(P1 + P2) - pl,b+1);
                if (sc > max) max = sc;
            }
        }
        score += max;
    }
    if (pl == P2)
    return -score;
    return score;
}
void AIPlay()
{
    int score;
    int max = -9999;
    int cx = 0, cy = 0;
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        if (ListBoard[0][x][y].player == null)
        {
            BoardToBoard(0,1);
            score = AIRepeat(x,y,P1,1);
            if (score > max)
            {
                max = score;
                cx = x;
                cy = y;
            }
        }
    }
    Drawchar(cx,cy,VX,P1);
    ListBoard[0][cx][cy].player = P1;
    ListBoard[0][cx][cy].value = VX;
    ListPlay[cplayed].New(cx,cy);
    cplayed++;
}
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y-i].player == pl) count++;
        else if (ListBoard[0][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y+i].player == pl) count++;
        else if (ListBoard[0][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y+i].player == pl) count++;
        else if (ListBoard[0][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y-i].player == pl) count++;
        else if (ListBoard[0][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
    
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListBoard[0][x-1][y-1].player = null;
            ListBoard[0][x-1][y-1].value = null;
        }
    }
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void DrawWin(int x, int y)
{
    int pl = ListBoard[0][x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        {   
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y-i].player == pl) Drawchar(x-i,y-i,ListBoard[0][x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y+i].player == pl) Drawchar(x+i,y+i,ListBoard[0][x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y+i].player == pl) Drawchar(x-i,y+i,ListBoard[0][x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y-i].player == pl) Drawchar(x+i,y-i,ListBoard[0][x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    {   
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    {   
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListBoard[0][x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListBoard[0][x/w][y/h].value = curSq.value;
            ListBoard[0][x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
            DrawInfo();
            WinGame();
            if (!wongame)
            {
                AIPlay();
                ChangePlayer();
            }
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
    
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    curSq.player = P2;
    curSq.value = VO;
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListBoard[0][x][y].value = null;
    ListBoard[0][x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
        Undo();
    }
    
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}
 

Dương Phạm 106

Cựu Kiểm soát viên | Cựu CTV CLB Địa lí
HV CLB Địa lí
Thành viên
8 Tháng năm 2019
1,991
4,238
471
Hà Nội
Trường THCS ...
Đây là code game có dùng trí tuệ nhân tạo. Mình thêm thuật toán AI và thay đổi code ở trên mọt chút là tạo được game này. Tuy chơi nó hơi ngu một tí nhưng mà vẫn thắng được một số người chơi ở mức cơ bản.
Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
const int bkc = 0; //background color = black
const int btnc = 6; // button color = orange
const int level = 2; //SET LEVEL HERE
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
       
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare ListBoard[level+1][col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
int AtkArray[9] = {0,1,2,7,100,200,210,220,230};
int DefArray[9] = {0,1,2,7,30,40,50,60,70};
/*----------------------------------------------------------------*/

/*--------------------------AI------------------------------------*/
void BoardToBoard(int b1, int b2)
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            ListBoard[b2][x][y].value = ListBoard[b1][x][y].value;
            ListBoard[b2][x][y].player = ListBoard[b1][x][y].player;
        }
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
int AIHori(int x, int y,int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIVerti(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIPCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y-i].player == player) count++;
        else if (ListBoard[cboard][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y+i].player == player) count++;
        else if (ListBoard[cboard][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y+i].player == player) count++;
        else if (ListBoard[cboard][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y-i].player == player) count++;
        else if (ListBoard[cboard][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRepeat(int x,int y,int pl,int b)
{
    int max = -9999;
    BoardToBoard(b-1,b);
    ListBoard[b][x][y].player = (P1 + P2) - pl;
    int scoreDef = DefArray[AIHori(x,y,b)] + DefArray[AIVerti(x,y,b)] + DefArray[AIPCross(x,y,b)] + DefArray[AIRCross(x,y,b)];
    ListBoard[b][x][y].player = pl;
    int scoreAtk = AtkArray[AIHori(x,y,b)] + AtkArray[AIVerti(x,y,b)] + AtkArray[AIPCross(x,y,b)] + AtkArray[AIRCross(x,y,b)];
    int score = scoreDef + scoreAtk;
    if (b < level)
    {
        int sc;
        FOR(x1,0,col-1)
        {
            FOR(y1,0,row-1)
            if (ListBoard[b][x1][y1].player == null)
            {
                sc = AIRepeat(x1,y1,(P1 + P2) - pl,b+1);
                if (sc > max) max = sc;
            }
        }
        score += max;
    }
    if (pl == P2)
    return -score;
    return score;
}
void AIPlay()
{
    int score;
    int max = -9999;
    int cx = 0, cy = 0;
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        if (ListBoard[0][x][y].player == null)
        {
            BoardToBoard(0,1);
            score = AIRepeat(x,y,P1,1);
            if (score > max)
            {
                max = score;
                cx = x;
                cy = y;
            }
        }
    }
    Drawchar(cx,cy,VX,P1);
    ListBoard[0][cx][cy].player = P1;
    ListBoard[0][cx][cy].value = VX;
    ListPlay[cplayed].New(cx,cy);
    cplayed++;
}
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y-i].player == pl) count++;
        else if (ListBoard[0][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y+i].player == pl) count++;
        else if (ListBoard[0][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y+i].player == pl) count++;
        else if (ListBoard[0][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y-i].player == pl) count++;
        else if (ListBoard[0][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
   
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListBoard[0][x-1][y-1].player = null;
            ListBoard[0][x-1][y-1].value = null;
        }
    }
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void DrawWin(int x, int y)
{
    int pl = ListBoard[0][x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        {  
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y-i].player == pl) Drawchar(x-i,y-i,ListBoard[0][x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y+i].player == pl) Drawchar(x+i,y+i,ListBoard[0][x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y+i].player == pl) Drawchar(x-i,y+i,ListBoard[0][x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y-i].player == pl) Drawchar(x+i,y-i,ListBoard[0][x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListBoard[0][x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListBoard[0][x/w][y/h].value = curSq.value;
            ListBoard[0][x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
            DrawInfo();
            WinGame();
            if (!wongame)
            {
                AIPlay();
                ChangePlayer();
            }
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
   
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    curSq.player = P2;
    curSq.value = VO;
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListBoard[0][x][y].value = null;
    ListBoard[0][x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
        Undo();
    }
   
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}
Hay vậy làm sao bạn biết mấy cái này
 

Dương Phạm 106

Cựu Kiểm soát viên | Cựu CTV CLB Địa lí
HV CLB Địa lí
Thành viên
8 Tháng năm 2019
1,991
4,238
471
Hà Nội
Trường THCS ...

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,621
744
21
Phú Yên
Trường THPT Lương Văn Chánh
  • Like
Reactions: Dương Phạm 106

anlong6@gmail.com

Học sinh chăm học
Thành viên
4 Tháng ba 2019
565
795
121
Nam Định
Trường học
Share vài trăm dòng Code game cho những bạn muốn lập trình game bằng C++. Ai có hứng tìm hiểu thì trao đổi với mình.
Demo game :D
View attachment 133641

Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
        int bkc = 0; //background color = black
        int btnc = 6; // button color = orange
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
      
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y-i].player == pl) count++;
        else if (ListSq[x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y+i].player == pl) count++;
        else if (ListSq[x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y+i].player == pl) count++;
        else if (ListSq[x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y-i].player == pl) count++;
        else if (ListSq[x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
  
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
  
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListSq[x-1][y-1].player = null;
            ListSq[x-1][y-1].value = null;
        }
    }
    curSq.player = P1;
    curSq.value = VX;
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
void DrawWin(int x, int y)
{
    int pl = ListSq[x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        { 
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y-i].player == pl) Drawchar(x-i,y-i,ListSq[x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y+i].player == pl) Drawchar(x+i,y+i,ListSq[x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y+i].player == pl) Drawchar(x-i,y+i,ListSq[x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y-i].player == pl) Drawchar(x+i,y-i,ListSq[x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListSq[x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListSq[x/w][y/h].value = curSq.value;
            ListSq[x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
  
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    { 
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    { 
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListSq[x][y].value = null;
    ListSq[x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
    }
  
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}

Lưu ý là phải cài thư viện Winbgim.h vào mới xài được nha....không thì lại bảo code mình lỗi :D
Hay lắm anh ạ nhưng còn cách nào lập trình ngắn hơn không ạ?
 

quân pro

Cựu CTV Confession
Thành viên
22 Tháng bảy 2017
1,262
3,224
356
Hà Nội
THPT Trần Phú - Hoàn Kiếm
Wow. Anh làm nhiều về các code ghê ha!
Giờ còn làm Cpp nữa.
Học Cpp thế nào hả anh?
 

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,621
744
21
Phú Yên
Trường THPT Lương Văn Chánh
Hay lắm anh ạ nhưng còn cách nào lập trình ngắn hơn không ạ?
Không có code nào là tối ưu hoàn toàn nha, mình mới học nên chỉ dùng những thuật toán CỰC KỲ ĐƠN GIẢN để viết thôi. Bạn có thể tự viết một thuật toán ngắn hơn xem sao
Wow. Anh làm nhiều về các code ghê ha!
Giờ còn làm Cpp nữa.
Học Cpp thế nào hả anh?
Lên mạng học nha em, dễ lắm.
 

dangtiendung1201

Cựu Mod Toán
Thành viên
24 Tháng mười hai 2018
1,272
1,359
191
20
Thái Bình
THCS Lương Thế VInh-Thành phố Thái Bình
Đây là code game có dùng trí tuệ nhân tạo. Mình thêm thuật toán AI và thay đổi code ở trên mọt chút là tạo được game này. Tuy chơi nó hơi ngu một tí nhưng mà vẫn thắng được một số người chơi ở mức cơ bản.
Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
const int bkc = 0; //background color = black
const int btnc = 6; // button color = orange
const int level = 2; //SET LEVEL HERE
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
       
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare ListBoard[level+1][col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
int AtkArray[9] = {0,1,2,7,100,200,210,220,230};
int DefArray[9] = {0,1,2,7,30,40,50,60,70};
/*----------------------------------------------------------------*/

/*--------------------------AI------------------------------------*/
void BoardToBoard(int b1, int b2)
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            ListBoard[b2][x][y].value = ListBoard[b1][x][y].value;
            ListBoard[b2][x][y].player = ListBoard[b1][x][y].player;
        }
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
int AIHori(int x, int y,int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[cboard][i][y].player == player) count++;
        else if (ListBoard[cboard][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIVerti(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[cboard][x][i].player == player) count++;
        else if (ListBoard[cboard][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIPCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y-i].player == player) count++;
        else if (ListBoard[cboard][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y+i].player == player) count++;
        else if (ListBoard[cboard][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRCross(int x,int y, int cboard)
{
    int player = ListBoard[cboard][x][y].player;
    int count = -1;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[cboard][x-i][y+i].player == player) count++;
        else if (ListBoard[cboard][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[cboard][x+i][y-i].player == player) count++;
        else if (ListBoard[cboard][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2 && count > opcount) return count - opcount;
    if (opcount >= 2) return 0;
    return count;
}
int AIRepeat(int x,int y,int pl,int b)
{
    int max = -9999;
    BoardToBoard(b-1,b);
    ListBoard[b][x][y].player = (P1 + P2) - pl;
    int scoreDef = DefArray[AIHori(x,y,b)] + DefArray[AIVerti(x,y,b)] + DefArray[AIPCross(x,y,b)] + DefArray[AIRCross(x,y,b)];
    ListBoard[b][x][y].player = pl;
    int scoreAtk = AtkArray[AIHori(x,y,b)] + AtkArray[AIVerti(x,y,b)] + AtkArray[AIPCross(x,y,b)] + AtkArray[AIRCross(x,y,b)];
    int score = scoreDef + scoreAtk;
    if (b < level)
    {
        int sc;
        FOR(x1,0,col-1)
        {
            FOR(y1,0,row-1)
            if (ListBoard[b][x1][y1].player == null)
            {
                sc = AIRepeat(x1,y1,(P1 + P2) - pl,b+1);
                if (sc > max) max = sc;
            }
        }
        score += max;
    }
    if (pl == P2)
    return -score;
    return score;
}
void AIPlay()
{
    int score;
    int max = -9999;
    int cx = 0, cy = 0;
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        if (ListBoard[0][x][y].player == null)
        {
            BoardToBoard(0,1);
            score = AIRepeat(x,y,P1,1);
            if (score > max)
            {
                max = score;
                cx = x;
                cy = y;
            }
        }
    }
    Drawchar(cx,cy,VX,P1);
    ListBoard[0][cx][cy].player = P1;
    ListBoard[0][cx][cy].value = VX;
    ListPlay[cplayed].New(cx,cy);
    cplayed++;
}
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListBoard[0][i][y].player == pl) count++;
        else if (ListBoard[0][i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListBoard[0][x][i].player == pl) count++;
        else if (ListBoard[0][x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y-i].player == pl) count++;
        else if (ListBoard[0][x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y+i].player == pl) count++;
        else if (ListBoard[0][x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListBoard[0][x][y].value == null) return false;
    int pl = ListBoard[0][x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListBoard[0][x-i][y+i].player == pl) count++;
        else if (ListBoard[0][x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListBoard[0][x+i][y-i].player == pl) count++;
        else if (ListBoard[0][x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
   
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListBoard[0][x-1][y-1].player = null;
            ListBoard[0][x-1][y-1].value = null;
        }
    }
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void DrawWin(int x, int y)
{
    int pl = ListBoard[0][x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        {  
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListBoard[0][i][y].player == pl) Drawchar(i,y,ListBoard[0][i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListBoard[0][x][i].player == pl) Drawchar(x,i,ListBoard[0][x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y-i].player == pl) Drawchar(x-i,y-i,ListBoard[0][x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y+i].player == pl) Drawchar(x+i,y+i,ListBoard[0][x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListBoard[0][x-i][y+i].player == pl) Drawchar(x-i,y+i,ListBoard[0][x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListBoard[0][x+i][y-i].player == pl) Drawchar(x+i,y-i,ListBoard[0][x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListBoard[0][x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListBoard[0][x/w][y/h].value = curSq.value;
            ListBoard[0][x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
            DrawInfo();
            WinGame();
            if (!wongame)
            {
                AIPlay();
                ChangePlayer();
            }
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
   
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    curSq.player = P2;
    curSq.value = VO;
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListBoard[0][x][y].value = null;
    ListBoard[0][x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
        Undo();
    }
   
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}
Em chạy bằng Code Blocks nó cứ báo lỗi redefinition of 'int right' ở thư viện winbgim.h ạ.
 

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,621
744
21
Phú Yên
Trường THPT Lương Văn Chánh
Em chạy bằng Code Blocks nó cứ báo lỗi redefinition of 'int right' ở thư viện winbgim.h ạ.
Code Block chỉ dùng để chạy code thuật toán thôi nha, em muốn test code này phải tải Dev c++ mới được. Những game anh làm và copy trên mạng đều phả dùng Dev c++. Code Block không dùng cho Game :D
 

Oxozone18

Học sinh mới
Thành viên
3 Tháng sáu 2022
1
0
1
24
Hà Nội
Share vài trăm dòng Code game cho những bạn muốn lập trình game bằng C++. Ai có hứng tìm hiểu thì trao đổi với mình.
Demo game :D
View attachment 133641

Mã:
#include <iostream>
#include <winbgim.h>
#define row 20
#define col 20
#define null 0
#define VX 1
#define VO 2
#define P1 4 //Red
#define P2 1 //Blue
#define GameSpeed 100

#define FOR(x,f,e) for(int x = f;x <= e;x++)
#define FORD(x,f,e) for(int x = f; x >= e;x--)
#define InBar(x,f,e) (x >= f && x <= e)
#define Click ismouseclick(WM_LBUTTONDBLCLK)
#define RClick ismouseclick(WM_RBUTTONDOWN)

const int wid = 500, hei = 500, Twid = 300 + 10;
        int bkc = 0; //background color = black
        int btnc = 6; // button color = orange
using namespace std;

/*-----------------------Variable------------------------------*/
class Tsquare
{
    public:
        int player;
        int value;
};
class TPlayed
{
    public:
        int x,y;
        void New(int, int);
       
};
void TPlayed::New(int px, int py)
{
    x = px; y = py;
}
Tsquare ListSq[col][row];
Tsquare curSq;
int w = wid / col;
int h = hei / row;
bool ex, wongame;
int cplayed;
TPlayed ListPlay[row*col];
/*----------------------------------------------------------------*/

/*-------------------------CheckGameWin--------------------------------*/
bool HoriWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,x,0)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,x+1,col-1)
    {
        if (ListSq[i][y].player == pl) count++;
        else if (ListSq[i][y].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool VertiWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    FORD(i,y,0)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    FOR(i,y+1,row-1)
    {
        if (ListSq[x][i].player == pl) count++;
        else if (ListSq[x][i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool PCrossWin(int x,int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y-i].player == pl) count++;
        else if (ListSq[x-i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,row-y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y+i].player == pl) count++;
        else if (ListSq[x+i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
bool RCrossWin(int x, int y)
{
    if (ListSq[x][y].value == null) return false;
    int pl = ListSq[x][y].player;
    int count = 0;
    int opcount = 0;
    int m = min(x,row-y);
    FOR(i,0,m)
    {
        if (ListSq[x-i][y+i].player == pl) count++;
        else if (ListSq[x-i][y+i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    m = min(col-x,y);
    FOR(i,1,m)
    {
        if (ListSq[x+i][y-i].player == pl) count++;
        else if (ListSq[x+i][y-i].player != null)
        {
            opcount++;
            break;
        }
        else break;
    }
    if (opcount >= 2) return false;
    if (count >= 5) return true;
    return false;
}
/*---------------------------------------------------------------------*/

/*---------------------------Create Game-------------------------------*/
void Drawtable(int color)
{
    setcolor(color);
    FOR(x,0,col)
    {
        line(x*w,0,x*w,wid);
    }
   
    FOR(y,0,row)
    {
        line(0,y*h,hei,y*h);
    }
}
void Createtable()
{
   
    FOR(x,1,col)
    {
        FOR(y,1,row)
        {
            ListSq[x-1][y-1].player = null;
            ListSq[x-1][y-1].value = null;
        }
    }
    curSq.player = P1;
    curSq.value = VX;
}
void ChangePlayer()
{
    if (curSq.player == P1)
    {
        curSq.player = P2;
        curSq.value = VO;
    }
    else if (curSq.player == P2)
    {
        curSq.player = P1;
        curSq.value = VX;
    }
}
void Drawchar(int c,int r,int value,int color)
{
    if (value == VX)
    {
        setcolor(color);
        line(c*w+3,r*h+3,(c+1)*w-3,(r+1)*h-3);
        line(c*w+3,(r+1)*h-3,(c+1)*w-3,r*h+3);
    }
    else if (value == VO)
    {
        setcolor(color);
        ellipse(c*w+w/2,r*h+h/2,0,360,w/2-3,h/2-3);
    }
}
void DrawWin(int x, int y)
{
    int pl = ListSq[x][y].player;
    if (HoriWin(x,y))
    {
        FORD(i,x,0)
        {  
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
        FOR(i,x+1,col-1)
        {
            if (ListSq[i][y].player == pl) Drawchar(i,y,ListSq[i][y].value,P1+P2);
            else break;
        }
    }
    if (VertiWin(x,y))
    {
        FORD(i,y,0)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
        FOR(i,y+1,row-1)
        {
            if (ListSq[x][i].player == pl) Drawchar(x,i,ListSq[x][i].value,P1+P2);
            else break;
        }
    }
    if (PCrossWin(x,y))
    {
        int m = min(x,y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y-i].player == pl) Drawchar(x-i,y-i,ListSq[x-i][y-i].value,P1+P2);
            else break;
        }
        m = min(col-x,row-y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y+i].player == pl) Drawchar(x+i,y+i,ListSq[x+i][y+i].value,P1+P2);
            else break;
        }
    }
    if (RCrossWin(x,y))
    {
        int m = min(x,row-y);
        FOR(i,0,m)
        {
            if (ListSq[x-i][y+i].player == pl) Drawchar(x-i,y+i,ListSq[x-i][y+i].value,P1+P2);
            else break;
        }
        m = min(col-x,y);
        FOR(i,1,m)
        {
            if (ListSq[x+i][y-i].player == pl) Drawchar(x+i,y-i,ListSq[x+i][y-i].value,P1+P2);
            else break;
        }
    }
}
/*---------------------------------------------------------------------*/

/*-------------------------------Method--------------------------------*/
void WinGame()
{
    FOR(x,0,col-1)
    {
        FOR(y,0,row-1)
        {
            if (HoriWin(x,y) || VertiWin(x,y) || PCrossWin(x,y) || RCrossWin(x,y))
            {
                wongame = true;
                DrawWin(x,y);
            }
        }
    }
}
void PlayinBoard(int x,int y)
{
    if (wongame) return;
    if (x/w <= col && y/h <= row)
    {
        if (ListSq[x/w][y/h].value == null)
        {
            Drawchar(x/w,y/h,curSq.value,curSq.player);
            ListSq[x/w][y/h].value = curSq.value;
            ListSq[x/w][y/h].player = curSq.player;
            ListPlay[cplayed].New(x/w,y/h);
            cplayed++;
            ChangePlayer();
        }
    }
}
void DrawMenu()
{
    setcolor(btnc); //orange
    setfillstyle(1,btnc);
    bar(wid + 75,25,wid + Twid - 85,75); // New Game
    bar(wid + 75,100,wid + Twid - 85,150); //Undo
    bar(wid + 75,175,wid + Twid - 85,225); //Exit
    rectangle(wid + 25,250,wid + Twid - 35,300); //Info
    rectangle(wid + 25,325,wid + Twid - 35,hei); //Help
   
    setcolor(14); //yellow
    setbkcolor(btnc);
    settextstyle(0,0,2);
    outtextxy(wid + 90,40,"NEW GAME");
    outtextxy(wid + 120,115,"UNDO");
    outtextxy(wid + 120,190,"EXIT");
}
void DrawInfo()
{
    setbkcolor(bkc);
    settextstyle(0,0,2);
    if (curSq.player == P1)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 1");
        outtextxy(wid + 50,280,"Character: X");
    }
    else if (curSq.player == P2)
    {  
        setcolor(15); //white
        outtextxy(wid + 50,260,"Player: 2");
        outtextxy(wid + 50,280,"Character: O");
    }
}
void DrawHelp()
{
    setcolor(btnc); //orange
    setbkcolor(bkc);
    settextstyle(0,0,2);
    outtextxy(wid + 25,325,"Help");
}
void NewGame()
{
    setbkcolor(bkc);//black
    cleardevice();
    Drawtable(2); // green
    Createtable();
    DrawMenu(); DrawInfo(); DrawHelp();
    cplayed = 0;
    ex = false;
    wongame = false;
}
void Undo()
{
    if (cplayed <= 0 || wongame) return;
    int x = ListPlay[cplayed-1].x;
    int y = ListPlay[cplayed-1].y;
    setfillstyle(1,bkc); //clear character
    bar(x*w+2,y*h+2,(x+1)*w-2,(y+1)*h-2);
    ListSq[x][y].value = null;
    ListSq[x][y].player = null;
    ChangePlayer();
    cplayed--;
}
void Exit()
{
    ex = true;
    cleardevice();
    setcolor(4);//red
    settextstyle(0,0,5); //red
    setbkcolor(bkc);
    outtextxy(50,hei/2 - 100,"THANK FOR PLAYING!");
    outtextxy(50,hei/2,"SEE YOU AGAIN!");
    delay(1500);
}
void Menu(int x,int y)
{
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,25,75))
    {
        NewGame();
    }
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,100,150))
    {
        Undo();
    }
   
    if (InBar(x,wid+75,wid+Twid-85) && InBar(y,175,225))
    {
        Exit();
    }
}
/*---------------------------------------------------------------------*/
int main()
{
    initwindow(wid+Twid,hei+15);
    NewGame();
    while (!ex)
    {
        delay(GameSpeed);
        if (Click)
        {
            int x,y;
            getmouseclick(WM_LBUTTONDBLCLK, x, y);
            Menu(x,y);
            PlayinBoard(x,y);
            DrawInfo();
            WinGame();
        }
    }
    closegraph();
    return 0;
}

Lưu ý là phải cài thư viện Winbgim.h vào mới xài được nha....không thì lại bảo code mình lỗi :D
cho mình hỏi cách cài thư viện Winbgim.h vào vscode với
 
Top Bottom