不能在最低可用点放置连接 4 个芯片

问题描述

代码全部基于Java编程语言,专门处理。我使用的网站是 OpenProcessing for school。

在我的 Connect 4 代码中,我试图将筹码放在尽可能低的可用位置。我有一个 checkSpace 整数,它应该通过获取该部分应该去的 x 和 y 值的颜色值来识别该空间是否可用。如果 x 和 y 值是白色,则放置该块。如果不是,则 y 值向上移动并检查该值。下面是代码供参考:

int checkSpace(int x) {
        for(int i = 5; i >= 0; i--) {
                if(get(x,150 + (i*100)) == color(255))
                    return i;
        }
        return -1;
}

下一段代码放置芯片本身,这是它不工作的地方。好吧,由于上面显示代码,它可能无法正常工作,所以如果这是问题,请告诉我。 无论如何,下一段代码以 void mouseReleased 开头,这样当用户单击鼠标时,他们就可以放置芯片。它以 if 语句继续,该语句确定轮到哪个玩家。然后继续使用 if 语句识别 x 值,检查鼠标位于网格的哪个部分。这是整个代码,很长,但请耐心等待:

void mouseReleased() {
    if(player1Turn) {
        if(mouseX <= 100) {
            if(checkSpace(50) != -1) {
                fill(255,0);
                ellipse((checkSpace(50)),50,50);
                player1Turn = !player1Turn;
                turn++;
            }
        }
        if(mouseX > 100 && mouseX <= 200){
            if(checkSpace(150) != -1) {
                fill(255,0);
                ellipse(150,(checkSpace(150)),50);
                player1Turn = !player1Turn;
                turn++;
            }
        
        }
        if(mouseX > 200 && mouseX <= 300) {
            if(checkSpace(250) != -1) {
                fill(255,0);
                ellipse(250,(checkSpace(250)),50);
                player1Turn = !player1Turn;
                turn++;
            }
        
        }
        if(mouseX > 300 && mouseX <= 400) {
            if(checkSpace(350) != -1) {
                fill(255,0);
                ellipse(3500,(checkSpace(350)),50);
                player1Turn = !player1Turn;
                turn++;
            }
        }
        if(mouseX > 400 && mouseX <= 500) {
            if(checkSpace(450) != -1) {
                fill(255,0);
                ellipse(450,(checkSpace(450)),50);
                player1Turn = !player1Turn;
                turn++;
            }
        }
        if(mouseX > 500 && mouseX <= 600) {
            if(checkSpace(550) != -1) {
                fill(255,0);
                ellipse(550,(checkSpace(550)),50);
                player1Turn = !player1Turn;
                turn++;
            }
        }
        if (mouseX > 600 && mouseX <= 700) {
            if(checkSpace(650) != -1) {
                fill(255,50);
                player1Turn = !player1Turn;
                turn++;
                }
        }
        
    }
    else {
        if(mouseX <= 100) {
            if(checkSpace(50) != -1) {
                fill(255,255,0);
                ellipse(50,(checkSpace(50)),50);
            
                player1Turn = !player1Turn;
                turn++;
            }
        
        }
        if(mouseX > 200 && mouseX <= 300) {
            if(checkSpace(250) != -1) {
                fill(255,50);
                player1Turn = !player1Turn;
                turn++;
            }
        
        }
        if(mouseX >300 && mouseX <= 400) {
            if(checkSpace(350) != -1) {
                fill(255,0);
                ellipse(350,0);
                ellipse(650,(checkSpace(650)),50);
                player1Turn = !player1Turn;
                turn++;
                }
        }
            
    
    }
}

我不确定我做错了什么。我真的被困住了,我不知道如何继续。感谢你给与我的帮助。如果问题不在上面的任何代码中,我会将整个代码粘贴在这里,供那些想要更深入了解的人使用。我还没有创建代码的获胜识别部分,所以到目前为止代码的唯一目的是将芯片放在最低的可用位置。代码如下:

int red = color(255,0);
int yellow = color(255,0);
boolean player1Turn = true;
int x = 7,y = 6;
int turn = 0;
int player = 0;
int grid[y][x] = {
    {0,9},{0,{9,9,9}
};

void setup(){
    size(700,600);
    background(255);
}
void draw(){
    for (int y = 0; y < grid.length-1; y++) {
       for (int x = 0; x < grid[y].length-1; x++) {
                 fill(255);
                 rect(x * 100,y * 100,99,99);
             }
    }
    playGame();
}
void playGame() {
    if(player1Turn){
        player1();
    }
    else {
        player2();
    }
}

void player1(){
    fill(255,100);
    if(mouseX <= 100) {
        ellipse(50,50);
    }
    else if(mouseX > 100 && mouseX <= 200){
        ellipse(150,50);
    }
    else if(mouseX > 200 && mouseX <= 300){
        ellipse(250,50);
    }
    else if(mouseX >300 && mouseX <= 400){
        ellipse(350,50);
    }
    else if(mouseX > 400 && mouseX <= 500){
        ellipse(450,50);
    }
    else if(mouseX > 500 && mouseX <= 600){
        ellipse(550,50);
    }
    else if(mouseX > 600 && mouseX <= 700){
        ellipse(650,50);
    }
}

void player2() {
    fill(255,100);
    if (mouseX > -1 && mouseX <= 100){
        ellipse(50,50);
    }
    else if(mouseX > 300 && mouseX <= 400){
        ellipse(350,50);
    }
}

int checkSpace(int x) {
        for(int i = 5; i >= 0; i--) {
                if(get(x,150 + (i*100)) == color(255))
                    return i;
        }
        return -1;
}

void mouseReleased() {
    if(player1Turn) {
        if(mouseX <= 100) {
            if(checkSpace(50) != -1) {
                fill(255,50);
                player1Turn = !player1Turn;
                turn++;
                }
        }
            
    
    }
}
    
    

再次感谢您提供的任何帮助。

解决方法

我刚刚看了处理库。 https://processing.org/reference/

Draw 是一个连续调用的函数,看起来你的 draw 函数会连续重绘整个板子。它基本上是一个运行的无限循环,然后一旦完成......再次运行等等。所以你想初始化设置中的所有内容,在开始时运行一次。

int ReAllocMem(pMassMem mem)
{
    int result_code=-1;
    //consider the current items
    size_t curr_size=0;
    int offset=0;    
    
    if(!mem) goto _mem_realloc_exit;
    
    curr_size=_msize(mem->mem)/sizeof(char);
    offset=mem->cur_plt-mem->mem;
    
    mem->mem=(char*)realloc(mem->mem,curr_size+FIRST_MEM_MAXSIZE);
    if(!mem->mem) {
        MessageBox(NULL,"ReAllocMem:mem cache failed","Serious trouble",MB_OK|MB_ICONINFORMATION);
        goto _mem_realloc_exit;
    }
        
    mem->row+=mem->row;
    mem->off_index=(int*)realloc(mem->off_index,sizeof(int)*mem->row*mem->col);
    if(!mem->off_index) {
        MessageBox(NULL,"ReAllocMem:offset cache failed",MB_OK|MB_ICONINFORMATION);
        goto _mem_realloc_exit;
    }
    
    mem->cur_plt=mem->mem+offset;
    
    result_code=0;
_mem_realloc_exit:
    if(result_code!=0) {
        ReleaseMassMem(mem);
    }
    return result_code;
}

因此,您需要将其移至设置中以避免重写画布而看不到任何更新。

接下来,您会注意到,如果您像这样让 playGame 处于平局状态:

void setup(){
    size(700,600);
    background(230);
    for (int y = 0; y < grid.length-1; y++) {
       for (int x = 0; x < grid[y].length-1; x++) {
             fill(245);
             rect(x * 100,y * 100,99,99);
       }
    }

}

然后省略号将不会被删除...那是因为您的 player1() 和 player2() 函数只放置省略号,并且由于 draw 不断重绘整个画布,所以您逃脱了它。
我认为一个不错的解决方案是,如果您创建一个额外的栏,那么现在高度为 6,并且只在那里生成椭圆,然后让绘制功能覆盖顶部栏。

接下来,您的 checkSpace 函数总是返回 -1。为了确定一个位置是否为空,您应该维护一个二维数组/矩阵来存储这些详细信息……一旦您检查是否有人赢了,这也会对您有所帮助。下面附上图片。

How the Matrix Should work

I mocked up a quick sketch on how I think the matrix should work

另外,因为你正在学习......这是完全有效的

void draw(){
    playGame();     
}

只要使用 else if 就不需要 if 中的额外条件。