跳到正文

休闲·娱乐

贪吃蛇代码

WightyIflia约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年8月24日
收录
2026年9月12日
更新
2026年10月4日
篇幅
约 9 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#include <cstdlib>
void clears(){
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}
#ifdef _WIN32
#include <conio.h>
bool keyPressed() { return _kbhit(); }
char getKey() { return _getch(); }
#else
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#endif
constexpr int maxn=1010;
ll N,M;
char k[maxn][maxn];
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll len1,len2;//%
ll P1x,P1y;
ll P2x,P2y;
vector<pair<ll,ll>> body1,body2;
void init()
{
    P1x=1;
    P1y=1;
    P2x=M;
    P2y=N;
    for(ll i=1;i<=N;++i)
        for(ll j=1;j<=M;++j)
    {
        k[i][j]='.';
        if(i==P1y && j==P1x) k[i][j]='@';
        if(i==P2y && j==P2x) k[i][j]='%';
    }
    while(1)
    {
        ll x__=rnd()%N+1;
        ll y__=rnd()%M+1;
        if(k[x__][y__]=='.')
        {
            k[x__][y__]='$';
            break;
        }
    }
    len1=1;
    for(ll i=1;i<len1;++i) body1.emplace_back(0,0);
    len2=1;
    for(ll i=1;i<len2;++i) body2.emplace_back(0,0);
    ll T=0;
    while(T--)
    {
        got:;
        ll x__=rnd()%N+1;
        ll y__=rnd()%M+1;
        if(k[x__][y__]=='.')
        {
            k[x__][y__]='?';
            continue;
        }
        goto got;
    }
}
void print()
{
    for(ll i=1;i<=N;++i)
    {
        for(ll j=1;j<=M;++j) cout << k[i][j];
        cout << endl;
    }
    cout << "Score1" << (len1>2?"s":"") << ": " << len1-1 << endl;
    cout << "Score2" << (len2>2?"s":"") << ": " << len2-1 << endl;
}
ll pglick=0;
ll countapple=1;
ll op1,op2;
void mn()
{
    char ch;
    string s;
    ll f1=P1x,f2=P1y;
    ll f3=P2x,f4=P2y;
    getline(cin,s);
    for(auto ch:s){
    if(ch=='w')
    {
        op1=1;
    }
    else if(ch=='s')
    {
        op1=2;
    }
    else if(ch=='a')
    {
        op1=3;
    }
    else if(ch=='d')
    {
        op1=4;
    }
    if(ch=='i')
    {
        op2=1;
    }
    else if(ch=='k')
    {
        op2=2;
    }
    else if(ch=='j')
    {
        op2=3;
    }
    else if(ch=='l')
    {
        op2=4;
    }
    }
    if(op1==1) P1y--;
    else if(op1==2) P1y++;
    else if(op1==3) P1x--;
    else if(op1==4) P1x++;
    if(op2==1) P2y--;
    else if(op2==2) P2y++;
    else if(op2==3) P2x--;
    else if(op2==4) P2x++;
    if(k[P1y][P1x]=='$')
    {
        k[P1y][P1x]='@';
        body1.emplace_back(0,0);
        len1++;
        countapple--;
    }
    if(k[P2y][P2x]=='$')
    {
        k[P2y][P2x]='%';
        body2.emplace_back(0,0);
        len2++;
        countapple--;
    }
    ll u,v;
    for(auto &i:body1)
    {
        u=i.first;
        v=i.second;
        i.first=f1;
        i.second=f2;
        f1=u;
        f2=v;
    }
    for(auto &i:body2)
    {
        u=i.first;
        v=i.second;
        i.first=f3;
        i.second=f4;
        f3=u;
        f4=v;
    }
    for(ll i=1;i<=N;++i)
    {
        for(ll j=1;j<=M;++j)
        {
            if(k[i][j]=='$' ||k[i][j]=='?') continue;
            k[i][j]='.';
            if(j==P1x && i==P1y) k[i][j]='@';
            if(j==P2x && i==P2y) k[i][j]='%';
        }
    }
    pglick++;
    if(pglick>=2 /*&& countapple<=9 */&& countapple+len1<N*M)
    {
        pglick-=2;
        while(1)
        {
            ll x__=rnd()%N+1;
            ll y__=rnd()%M+1;
            if(k[x__][y__]=='.')
            {
                k[x__][y__]='$';
                break;
            }
        }
        countapple++;
    }
    for(auto i:body1)
    {
        k[i.second][i.first]='#';
    }
    for(auto i:body2)
    {
        k[i.second][i.first]='O';
    }
}
ll ntgameover()
{
    if(P1x==P2x && P1y==P2y) return 0;
    if(P1x==M+1 || P1x==0 || P1y==0 || P1y==N+1) return 1;
    if(P2x==M+1 || P2x==0 || P2y==0 || P2y==N+1) return 2;
    for(auto i:body1)
    {
        if(i.first==P1x && i.second==P1y)
        {
            return 1;
        }
        if(i.first==P2x && i.second==P2y)
        {
            return 2;
        }
    }
    for(auto i:body2)
    {
        if(i.first==P1x && i.second==P1y)
        {
            return 1;
        }
        if(i.first==P2x && i.second==P2y)
        {
            return 2;
        }
    }
    return 3;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << "Please write the size of game" << endl;
    cin >> N >> M;
    cin.ignore();
    clears();
    init();
    ll t;
    while(t=ntgameover())
    {
        if(t!=3) break;
        clears();
        print();
        mn();
    }
    clears();
    if(!t) cout << "Head meet head";
    else if(t==1) cout << "P2 Wins";
    else if(t==2) cout << "P1 Wins";
}
原文